手指的下面
          時間長了,手指會痛
          posts - 3,  comments - 2,  trackbacks - 0
          子類繼承父類
              1:子類方法優先檢索并使用子類的方法
              2:子類與父類的屬性都是存在,引用類型是父類時就使用父類屬性,是子類就使用子類屬性
              3
          :調用父類方法(super.method()),父類方法中:
                  調用方法,優先搜索子類中是否有該方法,然后是父類的;
                  調用屬性,則是使用父類的屬性。
           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


          打印結果:

           

          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) 評論(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);
          }
          }  回復  更多評論
            
          # re: 類的繼承
          2008-08-20 22:30 | xunSir.lee
          @honeyjava
          謝謝,現在明白
          繼承時:
          1.子類與父類的屬性都是存在
          引用類型是父類時就使用父類屬性,是子類就使用子類屬性
          2.方法優先檢索并使用子類
          如果可以,能麻煩你解釋一下,它們的內里機制嗎?  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

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

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 澜沧| 买车| 津市市| 黎川县| 山东| 西华县| 东乌珠穆沁旗| 喀喇沁旗| 合山市| 定西市| 抚顺县| 五莲县| 墨江| 卓资县| 塔河县| 邢台市| 汾西县| 石嘴山市| 福建省| 会昌县| 七台河市| 突泉县| 泾川县| 梁山县| 且末县| 衡东县| 昌江| 哈巴河县| 玉溪市| 珲春市| 务川| 浙江省| 贡嘎县| 黄浦区| 湘潭县| 无极县| 呼伦贝尔市| 太保市| 上思县| 垫江县| 琼结县|