隨筆 - 4  文章 - 10  trackbacks - 0
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章分類

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          出處:http://www.aygfsteel.com/crazycy/archive/2006/10/11/74622.html

          < 示例1>

           


           1 class  Base {
           2      int  x  =   2 ;
           3      int  method() {
           4          return  x;
           5     }

           6 }

           7
           8 class  SubClass  extends  Base {
           9      int  x  =   3 ;
          10      int  method() {
          11          return  x;
          12     }

          13 }

          14
          15 public   class  Main 
          16          public   static   void  main(String[] args)  {
          17         Base b  =   new  SubClass ();
          18         System.out.println(b.x);
          19         System.out.println(b.method());
          20     }

          21 }

           


          2 , 3

           

           

          <練習(xí)>

           


           1 class   s1    {   
           2        public   String  s = " s1 " ;   
           3        public   String  get()   {   
           4            return   s;   
           5       }
             
           6 }
             
           7 class   s2   extends   s1    {   
           8       public    String   s = " s2 " ;   
           9        public    String   get()    {   
          10            return    s;   
          11       }
             
          12  }
             
          13 public     class    s   {   
          14        public     static     void    main(String[]   args)   {   
          15           s1  a    =     new   s2();   
          16           System.out.println(a.s);   
          17           System.out.println(a.get());     
          18       }
             
          19  }
            

           


           1 這個地方就是多態(tài)的一個陷阱;   
           2 多態(tài)是對方法而言的,不是對變量;   
           3 s1 a  =   new  s2();這里生成的對象是s1類的實(shí)例,但是是由s2類構(gòu)造的;   
           4     
           5   java中對變量的選擇是靜態(tài)的,對方法的選擇是動態(tài)的,是在運(yùn)行時決定的。(static除外)   
           6   運(yùn)行時實(shí)際上調(diào)用的是實(shí)例的方法,即s1的方法;但對于繼承(多態(tài)的一種方式),方法的定位是在動態(tài)執(zhí)行時選擇的,選擇實(shí)際構(gòu)造者,因此就出現(xiàn)了本題的現(xiàn)象了。   
           7
           8 另外:多態(tài)是對方法而言的,不是對變量;這樣說有些不嚴(yán)密,方法應(yīng)該有個修飾,就是除了final修飾的方法外,java中對函數(shù)的調(diào)用都是后期綁定,所謂的后期綁定就是動態(tài)選擇
           9
          10 摘自 崔毅解答csdn疑問時給出的分析
          11
          12

           


          示例2>

           


           1 class  Base {
           2      private   final   void  f() {
           3         System.out.println( " Base.f() " );
           4     }

           5 }

           6
           7 class  Derived  extends  Base {
           8      public   final   void  f() {
           9         System.out.println( " Derived.f() " );
          10     }

          11 }

          12
          13 public   class  Main 
          14          public   static   void  main(String[] args)  {
          15         Derived op1  =   new  Derived();
          16         Base op2 = op1;
          17         op1.f();
          18         op2.f();
          19     }

          20 }

          21

           


          op2.f();

           


          <示例3>

           


           1 class    Parent {   
           2        private     void    method1() {   
           3           System.out.println( " Parent's   method1() " );   
           4       }
             
           5        public     void    method2() {   
           6           System.out.println( " Parent's   method2() " );   
           7           method1();   
           8       }
             
           9   }
             
          10    class    Child    extends    Parent {   
          11        public     void    method1() {   
          12          System.out.println( " Child's   method1() " );   
          13       }
             
          14        public     static     void    main(String   args[]) {   
          15           Parent   p    =     new    Child();   
          16           p.method2();   
          17       }
             
          18   }
             

           


          1 答案是:prints:   parent’s   method2()     parent’s   method1()   
          2 如果把父類中method1改成public,那么答案是
          3 prints:   parent’s   method2()     child’s   method1()

           


          分析

          多態(tài):

          Java 中的函數(shù),除了聲明外 final 的外,都是后期綁定。

          所謂綁定是建立“函數(shù)調(diào)用”和“函數(shù)本體”的關(guān)聯(lián)。、

          所謂的后期綁定是指執(zhí)行時根據(jù)對象類別而進(jìn)行

          多態(tài)僅僅對函數(shù)而言,不對變量而言;

          變量的訪問依賴于編譯期引用指向的類型;

          方法的訪問依賴于執(zhí)行期對象的類型

          向上轉(zhuǎn)型后,調(diào)用某個函數(shù),若 derived class overriding 了該函數(shù),則會調(diào)用該 derived class 中的函數(shù),否則會調(diào)用 base class 中的函數(shù)

          向上轉(zhuǎn)型后,只能調(diào)用 base class 中被 derived class overriding 的函數(shù),不能調(diào)用 derived class extend 函數(shù)。

          向上轉(zhuǎn)型后,只能調(diào)用 base class 中的方法,不能調(diào)用 derived class 中的擴(kuò)展方法 
           

           


           1 public   class  CalC  {
           2      void  amethod() {
           3         System.out.println( " CalC.amethod " );
           4     }

           5     CalC() {
           6         amethod();
           7         System.out.println( " Hu? " );
           8     }

           9      public   static   void  main(String[] args)  {
          10         CalC cc  =   new  CalChild();
          11         cc.amethod();
          12     }

          13 }

          14 class  CalChild  extends  CalC {
          15      void  amethod() {
          16         System.out.println( " CalChild.amethod " );
          17     }

          18 }

           


          1 output:
          2 CalChild.amethod
          3 Hu ?
          4 CalChild.amethod
          5 為什么CalC Constructor調(diào)用的不是自己的amethod()呢

           


          1 方法在內(nèi)存中只有一個備份,所以的對象都共享這個備份,為了區(qū)分開到底是哪個對象在調(diào)用這個方法,關(guān)鍵的地方就是this的使用。this把調(diào)用方法的上下文對應(yīng)到當(dāng)前對象上。
          2
          3 第二,調(diào)用java中的所有成員變量或者成員函數(shù)都隱含了this。
          4
          5 所以這個地方就很明了了:構(gòu)造子類,this指針代表的當(dāng)前對象是子類實(shí)例,子類實(shí)例為啥不調(diào)用自己overriding的方法呢?!
          6
          posted on 2007-09-04 17:09 冬天出走的豬 閱讀(237) 評論(0)  編輯  收藏 所屬分類: j2se
          主站蜘蛛池模板: 定结县| 阳新县| 高碑店市| 梁河县| 丹棱县| 若羌县| 四会市| 陇南市| 正定县| 霍林郭勒市| 郸城县| 贵溪市| 宜昌市| 海宁市| 旌德县| 宁阳县| 巴彦县| 晋城| 铜川市| 荣成市| 徐州市| 定兴县| 天全县| 金湖县| 峨山| 儋州市| 徐汇区| 合山市| 嫩江县| 韶山市| 多伦县| 湖南省| 华阴市| 南皮县| 珠海市| 安顺市| 吉木乃县| 五常市| 象山县| 固镇县| 全南县|