java 继承与多态练习题目

时间:2023-02-12 21:23:53
//请写出以下程序的运行结果....
public class A {
public String show(D obj) {
return ("A and D");
}


public String show(A obj) {
return ("A and A");
}


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


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 {

}





ans:

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