【发布时间】:2014-08-03 00:30:45
【问题描述】:
我有 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() 方法的两次调用都与Child 对象调用了不同的方法。一方面,它在编译时和其他运行时绑定。这对我来说听起来有点奇怪。你会怎么解释这个?
【问题讨论】:
-
呃,在我看来,这两种情况下都是编译时绑定……看看方法重载是如何解决的,应该有一个答案漂浮在某个地方
-
两种情况的编译时间...
标签: java inheritance polymorphism overloading