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

© jia7604112 中级黑马   /  2015-11-13 22:58  /  489 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  今天老师讲了多态讲完后给这个面试题看的我都晕了求大神解答,解释最好能详细点
  1. class A {  
  2.         public String show(D obj){  
  3.                 return ("A and D");  
  4.         }   
  5.         public String show(A obj){  
  6.                 return ("A and A");  
  7.         }   
  8. }   
  9. class B extends A{  
  10.         public String show(B obj){  
  11.                 return ("B and B");  
  12.         }  
  13.         public String show(A obj){  
  14.                 return ("B and A");  
  15.         }   
  16. }  
  17. class C extends B{}   
  18. class D extends B{}

  19. class  DynamicTest
  20. {       
  21.         public static void main(String[] args){       
  22.                 A a1 = new A();  
  23.                 A a2 = new B();  
  24.                 B b = new B();  
  25.                 C c = new C();   
  26.                 D d = new D();   
  27.                 System.out.println(a1.show(b));
  28.                 System.out.println(a1.show(c));
  29.                 System.out.println(a1.show(d));  
  30.                 System.out.println(a2.show(b));   
  31.                 System.out.println(a2.show(c));
  32.                 System.out.println(a2.show(d));  
  33.                 System.out.println(b.show(b));   
  34.                 System.out.println(b.show(c));   
  35.                 System.out.println(b.show(d));
  36.         }
  37. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马