私のテストコード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);
}
}