<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    常用鏈接

    留言簿(1)

    隨筆檔案

    文章分類

    文章檔案

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    出處:http://m.tkk7.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

     

     

    <練習>

     


     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 這個地方就是多態的一個陷阱;   
     2 多態是對方法而言的,不是對變量;   
     3 s1 a  =   new  s2();這里生成的對象是s1類的實例,但是是由s2類構造的;   
     4     
     5   java中對變量的選擇是靜態的,對方法的選擇是動態的,是在運行時決定的。(static除外)   
     6   運行時實際上調用的是實例的方法,即s1的方法;但對于繼承(多態的一種方式),方法的定位是在動態執行時選擇的,選擇實際構造者,因此就出現了本題的現象了。   
     7
     8 另外:多態是對方法而言的,不是對變量;這樣說有些不嚴密,方法應該有個修飾,就是除了final修飾的方法外,java中對函數的調用都是后期綁定,所謂的后期綁定就是動態選擇
     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()

     


    分析

    多態:

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

    所謂綁定是建立“函數調用”和“函數本體”的關聯。、

    所謂的后期綁定是指執行時根據對象類別而進行

    多態僅僅對函數而言,不對變量而言

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

    方法的訪問依賴于執行期對象的類型

    向上轉型后,調用某個函數,若 derived class overriding 了該函數,則會調用該 derived class 中的函數,否則會調用 base class 中的函數

    向上轉型后,只能調用 base class 中被 derived class overriding 的函數,不能調用 derived class extend 函數。

    向上轉型后,只能調用 base class 中的方法,不能調用 derived class 中的擴展方法 
     

     


     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調用的不是自己的amethod()呢

     


    1 方法在內存中只有一個備份,所以的對象都共享這個備份,為了區分開到底是哪個對象在調用這個方法,關鍵的地方就是this的使用。this把調用方法的上下文對應到當前對象上。
    2
    3 第二,調用java中的所有成員變量或者成員函數都隱含了this。
    4
    5 所以這個地方就很明了了:構造子類,this指針代表的當前對象是子類實例,子類實例為啥不調用自己overriding的方法呢?!
    6
    posted on 2007-09-04 17:09 冬天出走的豬 閱讀(244) 評論(0)  編輯  收藏 所屬分類: j2se
    主站蜘蛛池模板: 免费做爰猛烈吃奶摸视频在线观看| 国产在线精品观看免费观看| 69式国产真人免费视频| 亚洲Av永久无码精品三区在线 | 亚洲一级免费毛片| 亚洲精品高清国产一久久| 水蜜桃视频在线观看免费播放高清| 亚洲国产天堂久久久久久| 亚洲一级片免费看| 亚洲无线观看国产精品| 18禁在线无遮挡免费观看网站| 国产AV无码专区亚洲Av| 8x成人永久免费视频| 亚洲免费福利视频| 毛片免费视频播放| 亚洲AV无码专区在线电影成人 | 亚洲国产精华液网站w| 久久免费公开视频| 亚洲女人18毛片水真多| 在线免费视频一区| 国产AV无码专区亚洲AV琪琪| 国产日韩成人亚洲丁香婷婷| 日本免费高清视频| 精品亚洲456在线播放| 国产成人精品免费视频大全五级 | 亚洲高清免费在线观看| 久久久久久国产精品免费免费| 亚洲av无码成人影院一区| 国产精品亚洲综合专区片高清久久久| 两个人看的www免费| 91午夜精品亚洲一区二区三区| 免费看a级黄色片| 97在线视频免费公开视频| 亚洲欧洲日韩国产| 免费A级毛片在线播放不收费| 成全视频高清免费观看电视剧| 亚洲综合久久一本伊伊区| 亚洲偷自拍拍综合网| 亚洲国产人成网站在线电影动漫| 蜜桃AV无码免费看永久| 羞羞视频网站免费入口|