Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 动态方法分派/运行时多态性_Java - Fatal编程技术网

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());


    }
}
希望如此,它将帮助您理解运行时多态性