A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© scalar 中级黑马   /  2016-5-22 22:40  /  496 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一道java多态题

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{}  

public class test{

        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));
               
        }
}



输出结果:
A and A
A and A
A and D
B and A
B and A
A and D
B and B
B and B
A and D
对以上的输出不是很理解,特别是第4个,有没有大神能帮帮我?

1 个回复

倒序浏览
我也看疯掉啦
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马