我对java多态性感到困惑。在动态方法绑定中,jvm 在运行时决定必须调用哪个类方法。假设我有 A、B 和 C 三个班级。
class A{
int get(){
return 10;
}
int getParent(){
return 10;
}
}
class B extends A
{
int get(){
return 20;
}
}
public class C
{
public static void main(String args[])
{
A a = new A();
A a1 = new B();
System.out.println(a.get());/////////////////////////LINE1
System.out.println(a1.get ());////////////////////////LINE2
System.out.println(a.getParent());////////////////////////LINE3
}
}
我在编译时和运行时绑定的第 1 行和第 3 行有混淆。在第 3 行中,它是 a.getParent(),并且此方法仅在父类中,因此它必须在运行时决定。
在第 1 行中,引用和对象都来自同一个类,所以它必须再次决定。
请向我发送运行时和编译时绑定如何工作的任何好的链接。