手指的下面
          時間長了,手指會痛
          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 閱讀(202) 評論(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)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 永春县| 寻乌县| 建宁县| 陵川县| 双江| 鄄城县| 中卫市| 元谋县| 林西县| 本溪市| 内黄县| 大姚县| 盐源县| 泾源县| 湾仔区| 汾阳市| 新晃| 武乡县| 红桥区| 澳门| 平湖市| 洛扎县| 崇州市| 望城县| 永嘉县| 侯马市| 江城| 衡东县| 广水市| 磴口县| 孟州市| 读书| 文昌市| 巢湖市| 区。| 定兴县| 灵川县| 都兰县| 姜堰市| 理塘县| 高台县|