手指的下面
          時(shí)間長了,手指會(huì)痛
          posts - 3,  comments - 2,  trackbacks - 0
          子類繼承父類
              1:子類方法優(yōu)先檢索并使用子類的方法
              2:子類與父類的屬性都是存在,引用類型是父類時(shí)就使用父類屬性,是子類就使用子類屬性
              3
          :調(diào)用父類方法(super.method()),父類方法中:
                  調(diào)用方法,優(yōu)先搜索子類中是否有該方法,然后是父類的;
                  調(diào)用屬性,則是使用父類的屬性。
           1class Tree {
           2    int i = 3, j = 5;
           3
           4    public Tree() {
           5        this.show("Tree");
           6    }

           7
           8    protected void show(String s) {
           9        System.out.println(s);
          10    }

          11
          12    public void t() {
          13        this.show("Tree, t");
          14        System.out.println(this.i);
          15        System.out.println(this.j);
          16    }

          17}

          18
          19class Leaf extends Tree {
          20    int i = 10;
          21
          22    public Leaf () {
          23        this.show("Leaf");
          24    }

          25
          26    public void show(String s) {
          27        System.out.println(s + "\t\tLeaf");
          28    }

          29
          30    public void t() {
          31        show("Leaf, t");
          32        System.out.println(this.i);
          33        System.out.println(this.j);
          34        System.out.println();
          35        super.t();
          36    }

          37
          38    public static void main(String args[]) {
          39        Leaf l = new Leaf();
          40        l.t();
          41
          42        System.out.println("------------------");
          43        System.out.println(l.i);
          44        Tree t = new Leaf();
          45        l = (Leaf) t;
          46        System.out.println(t.i);
          47        System.out.println(l.i);
          48    }

          49}

          50


          打印結(jié)果:

           

          Tree            Leaf
          Leaf            Leaf
          Leaf, t         Leaf
          10
          5

          Tree, t         Leaf
          3
          5
          ------------------
          10
          Tree            Leaf
          Leaf            Leaf
          3
          10

          posted on 2008-08-18 22:13 xunSir.lee 閱讀(203) 評(píng)論(2)  編輯  收藏 所屬分類: Java

          FeedBack:
          # re: 類的繼承
          2008-08-19 23:47 | honeyjava
          沒有子類覆蓋父類屬性一說

          /**
          * Created by IntelliJ IDEA.
          * User: Administrator
          * Date: 2008-8-19
          * Time: 23:34:32
          * To change this template use File | Settings | File Templates.
          */

          class Drived {
          public int f = 2;


          }

          public class Foo extends Drived {
          public int f = 1;


          public static void main(String[] args) {

          Drived foo = new Foo();
          Foo foofoo = (Foo) foo;
          System.out.println(foo.f);
          System.out.println(foofoo.f);
          }
          }  回復(fù)  更多評(píng)論
            
          # re: 類的繼承
          2008-08-20 22:30 | xunSir.lee
          @honeyjava
          謝謝,現(xiàn)在明白
          繼承時(shí):
          1.子類與父類的屬性都是存在
          引用類型是父類時(shí)就使用父類屬性,是子類就使用子類屬性
          2.方法優(yōu)先檢索并使用子類
          如果可以,能麻煩你解釋一下,它們的內(nèi)里機(jī)制嗎?  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          • 1.?re: 類的繼承
          • 評(píng)論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
          • --xunSir.lee
          • 2.?re: 類的繼承
          • 評(píng)論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
          • --honeyjava

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 隆安县| 浏阳市| 改则县| 上林县| 井冈山市| 博客| 巴东县| 墨玉县| 姜堰市| 涡阳县| 若尔盖县| 塔城市| 辉南县| 怀化市| 宁德市| 天台县| 金门县| 鄢陵县| 罗山县| 六安市| 武川县| 积石山| 资兴市| 安吉县| 南充市| 明溪县| 南木林县| 鹿邑县| 临潭县| 崇州市| 鹤峰县| 夏河县| 富阳市| 阳山县| 宁河县| 和顺县| 田东县| 江油市| 犍为县| 石门县| 金溪县|