Java 方法重载 + 双重调度
2022-09-03 00:50:01
任何人都可以详细解释在我的测试代码中处理实例时调用重载方法的原因吗?print(Parent parent)
Child
这里涉及Java中虚拟方法或方法重载/解析的任何特性?对 Java Lang Spec 有任何直接引用吗?哪个术语描述了这种行为?多谢。
public class InheritancePlay {
public static class Parent {
public void doJob(Worker worker) {
System.out.println("this is " + this.getClass().getName());
worker.print(this);
}
}
public static class Child extends Parent {
}
public static class Worker {
public void print(Parent parent) {
System.out.println("Why this method resolution happens?");
}
public void print(Child child) {
System.out.println("This is not called");
}
}
public static void main(String[] args) {
Child child = new Child();
Worker worker = new Worker();
child.doJob(worker);
}
}