黑马程序员技术交流社区

标题: 面试题求解 [打印本页]

作者: jia7604112    时间: 2015-11-13 22:58
标题: 面试题求解
  今天老师讲了多态讲完后给这个面试题看的我都晕了求大神解答,解释最好能详细点
  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. }
复制代码







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2