多態(tài)坑題

class A {  
     public String show(D obj){  
            return ("A and D");  
     }   
     public String show(A obj){  
            return ("A and A");  
     }   
}   
class B extends A{  
    public String show(B obj){  
            return ("B and B");  
     }  
     public String show(A obj){  
            return ("B and A");  
     }   
     
}  
class C extends B{}   
class D extends B{}

class  DynamicTest
{   
    public static void main(String[] args){
    
    A a1 = new A();  
    A a2 = new B();  
    B b = new B();  
    C c = new C();   
    D d = new D();   
    System.out.println(a1.show(b));  
    System.out.println(a1.show(c));  
    System.out.println(a1.show(d));  

    System.out.println(a2.show(b));                                         
    System.out.println(a2.show(c));  
    System.out.println(a2.show(d));  
    
    System.out.println(b.show(b));   
    System.out.println(b.show(c));   
    System.out.println(b.show(d));   

實際上這里涉及方法調用的優(yōu)先級問題,優(yōu)先級由高到低依次為:

this.show()--->super.show()-->this.show((super)0)-->super.show((super)0)


    System.out.println(a1.show(b));       A and A
    System.out.println(a1.show(c));       A and A
    System.out.println(a1.show(d));       A and D

    System.out.println(a2.show(b));       B and A                                  
    System.out.println(a2.show(c));       B and A
    System.out.println(a2.show(d));       A and D
    
    System.out.println(b.show(b));        B and B
    System.out.println(b.show(c));        B and B
    System.out.println(b.show(d));        A and D
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 很多人生病,愿意用湯滋補身體,說營養(yǎng)。其實湯里大多是脂肪,所以有飽腹感。 很多人愿意用海參調養(yǎng)身體,甚至...
    8ad130a09427閱讀 218評論 4 1
  • 八十年代《上海灘》熱播,許文強的白圍巾瘋魔,當時物資還算貧乏,但可以回家殃及媽媽織一條,掛在脖子上甩來甩去招搖過街...
    陳廠長閱讀 490評論 2 1
  • 最近宅著看書,研究《思維導圖》、《啟動大腦》、《快速閱讀》等相關的技能,形影相吊中稍感枯燥,另外也貪心的想有更多的...
    天外來客人閱讀 248評論 0 0
  • 【武夷巖茶】您知道那么多品種武夷茶的始祖是誰嗎? ——菜茶 「什么是菜茶?」 武夷山的;土生土長的;有性繁殖的;野...
    鼎典美育婷婷閱讀 270評論 0 0