<分区>
我有 3 个类 GrandParent
、Parent
和 Child
,其中
Child extends Parent
和 Parent extends GrandParent
public class Main {
void test(GrandParent gp){System.out.println("GrandParent");}
void test(Parent p){System.out.println("Parent");}
public static void main(String args[]){
GrandParent obj = new Child();
Main mainObj = new Main();
mainObj.test(obj); // This calls test(GrandParent gp)
mainObj.test(new Child()); // This calss test(Parent gp)
}
}
在上面的代码中,对 test()
方法的 2 次调用都使用 Child
对象调用了不同的方法。其中一个是编译时绑定(bind),另一个是运行时绑定(bind)。这对我来说听起来有点奇怪。你会如何解释这一点?