网上偶然看到这道题,顿时惊为天人。出这道题的绝对是牛人。我也是花了三个小时才搞明白,不知道大家需要多长时间。
会了这个,基本上继承,多态,重载,重写都没啥大问题了。所以做这道题之前大家最好再温习一遍这四个知识点。若是能一眼算出结果,我也只能膝盖送上。
//爷类
public class A {
public String show(D obj){
return ("A and D");
}
public String show(A obj){
return ("A and A");
}
}
//父类
public class B extends A{
public String show(B obj){
return ("B and B");
}
public String show(A obj){
return ("B and A");
}
}
//孙类
public class C extends B{
}
//孙类
public class D extends B{
}
//测试类。关于输出,希望大家最好一次只去掉一个注释,这才不会被搞晕。
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)); //9
}
//最终结果:
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
//解释;
/*
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123