Object1无法访问method2,我也不明白为什么。为什么object1不能访问method2,而object2可以访问method1?
ii给出了编译错误,但是我似乎无法找出原因...
class A {
public String method1() {
return "method1";
}
}
class B extends A {
public String method1() {
return super.method1()+"B";
}
}
class C extends A {
public String method2() {
return "method2";
}
}
class D extends B {
public String method1() {
return super.method1()+"D";
}
}
class Test {
public static void main(String[] args) {
A object1 = new C();
B object2 = new D();
D object3 = new D();
System.out.println(object1.method1()); // i
System.out.println(object1.method2()); // ii
System.out.println(object2.method1()); // iii
System.out.println(object3.method1()); // iv
}
}
//i - Will print 'method1'
//ii - Gives a compilation error
//iii - Will print 'method1BD'
//iv - Will print 'method1BD'
非常感谢您的帮助。