public class Exam1 {
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("(1)" + a1.show(b));//非多态,a1调用show()导致向上类型转换,-> A and A
//多态,运行时看右边的类,B中有两个show()方法还有从父类继承的,但被覆盖一个,所以调用从父类继承的->A and D
System.out.println("(2)" + a2.show(d));
//非多态--没有完全适用于C对象的,所以只能向上类型转换,调用B obj ->B and B
System.out.println("(3)" + b.show(c));
//非多态 --有三个方法,从父类继承的D obj ,适用于D obj,所以 -> A and D
System.out.println("(4)" + b.show(d));
}
}
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{
}