我不确定我是否对双重调度有错误的想法。但这就是我的想法:
class A{
void testA( B obj ){
System.out.println( "A-Parent" );
obj.testB();
}
}
class AChild extends A{
void testA( B obj ){
System.out.println( "A-Child" );
obj.testB();
}
}
class B{
void testB(){
System.out.println( "B-Parent" );
}
}
class BChild extends B{
void testB(){
System.out.println( "B-Child" );
}
}
class Launcher{
public static void main(){
A objA = new AChild();
B objB = new BChild();
objA.testA(objB);
}
}
What I expected:
A-Child
B-Parent
Actual output:
A-Child
B-Child // How ???????
由于Javas Single diapatch,我认为Java在运行时将objA正确解析为Achild,而在编译时将参数解析为B。请告诉我哪里弄错了?