Java 动态方法分派/运行时多态性
在运行时如何以及何时创建对象?动态绑定发生在哪里?你能给出一个实时的例子吗? 我知道它在运行时根据对象类型将方法调用绑定到方法实现!真的想看看它是怎么发生的吗? 回答与图片将是伟大的Java 动态方法分派/运行时多态性,java,Java,在运行时如何以及何时创建对象?动态绑定发生在哪里?你能给出一个实时的例子吗? 我知道它在运行时根据对象类型将方法调用绑定到方法实现!真的想看看它是怎么发生的吗? 回答与图片将是伟大的 非常感谢:)此示例将帮助您理解动态绑定 class Parent{ int value = 10; public void setValue(int value) { this.value = value; } public int getValue() {
非常感谢:)此示例将帮助您理解动态绑定
class Parent{
int value = 10;
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
class Child extends Parent{
int value =20;
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
public class RunTimePolymorphism {
public static void main(String[] args) {
Child child = new Child();
child.setValue(100);
System.out.println(child.getValue());
Child child1 = new Child();
System.out.println(child1.getValue());
Parent parent = new Child();
parent.setValue(200);
System.out.println(parent.getValue());
Parent parent1 = new Child();
System.out.println(parent1.getValue());
}
}
希望如此,它将帮助您理解运行时多态性