写了有段时间java了,回头看看最基础的继承问题,挺有意思,把之前不懂的理顺了
public class App9 {
public String show(D obj) {
return "App and D";
}
public String show(App9 obj) {
return "App and App";
}
}
class B extends App9 {
public String show(B obj) {
return "B and B";
}
@Override
public String show(App9 obj) {
return "B and A";
}
}
class C extends B {
public String show(C obj) {
return "C AND C";
}
@Override
public String show(App9 obj) {
return "C AND A";
}
}
class D extends B {
@Override
public String show(D d) {
return "D AND D";
}
@Override
public String show(App9 a) {
return "A AND A";
}
}
public class App10 {
public static void main(String[] args) {
App9 a1 = new App9();
App9 a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println(a1.show(b));// print App AND App
System.out.println(a1.show(c));// print App AND App
System.out.println(a1.show(d));// print APP AND D
System.out.println(a2.show(b));// print B AND A
System.out.println(a2.show(c));// print B AND A
System.out.println(a2.show(d));// print APP AND D
System.out.println(b.show(b));// print B AND B
System.out.println(b.show(c));// print B AND B
System.out.println(b.show(d));// print APP AND D
}
}
如果你对打印的输出全部理解,那也就理解了继承这部分内容了