参数化方法的动态多态性

时间:2015-10-23 04:05:59

标签: java polymorphism parametric-polymorphism

我想对动态多态性有一些清晰的认识。 当子类中的方法被覆盖和重载时,我无法找出方法调用。

这是Parent类:

父类:

public class Parent {

    void print(Parent parent){
        System.out.println("I am parent classes only print method.");
    }
}

儿童班:

public class Child extends Parent {

    void print(Child child) {
        System.out.println("I am child class' child print method.");
    }


    void print(Parent parent) {
        System.out.println("I am Child class' parent print method ");
    }


}

这是调用者类。

public class Caller {

    public static void main(String[] args) {

        Parent p = new Parent();
        Child c = new Child();
        Parent pc = new Child();

        p.print(p);
        p.print(c);
        p.print(pc);

        c.print(p);
        c.print(c);
        c.print(pc);

        pc.print(p);
        pc.print(c);
        pc.print(pc);

    }

}

我可以在控制台中看到输出,但无法理解方法调用背后的原因。

3 个答案:

答案 0 :(得分:1)

工作很简单 - 重载在编译时解析,覆盖在运行时解析(多态)。

所以,让我们看看每个方法调用中会发生什么......

我们会忽略使用Parent p = new Parent();的来电,因为它没有超载或覆盖,所有方法调用都将直接使用父级的单一方法"I am parent classes only print method."

另请注意,编译器只关心变量的引用类型。 并且运行时仅关注实际对象的类型

因此,在Parent pc = new Child()语句中,pc上的任何编译时决定都会引用Parent和任何运行时决定在pc上会引用Child

这是其他方法调用的逻辑,

c.print(p);
//Compiler resolves that `print(Parent)` method should be called.
//Runtime resolves that child objects method should be called.
//Prints "I am Child class' parent print method "

c.print(c);
//Compiler resolves that `print(Child)` method should be called.
//Runtime resolves that child objects method should be called.
//Prints "I am Child class' child print method "

c.print(pc);
//Compiler resolves that `print(Parent)` method should be called.
//Runtime resolves that child objects method should be called.
//Prints "I am Child class' parent print method "

pc.print(p);
//Compiler resolves that `print(Parent)` method should be called.
//Runtime resolves that child objects method should be called.
//Prints "I am Child class' parent print method "

pc.print(c); //PAY ATTENTION TO THIS...
//Compiler resolves that `print(Parent)` method should be called.
// This is because PC is Parent type reference and compiler doesn't find `print(Child)` in Parent class, so it uses `print(Parent)`.
//Runtime resolves that child objects method should be called.
//Prints "I am Child class' parent print method "

pc.print(pc);
//Compiler resolves that `print(Parent)` method should be called.
// This is because Compiler knows only about the variable's reference type (And PC is of type Parent). Hence `print(Parent)` would be chosen.
//Runtime resolves that child objects method should be called.
//During runtime, the type of the actual object is used. And PC is referring to an Child object... So `pc.print(...)` will call the child's method.
//Prints "I am Child class' parent print method "

答案 1 :(得分:0)

此问题看起来像是difference-between-method-overloading-and-overriding

的副本

覆盖(多态)和重载之间存在核心差异。

覆盖(多态)在运行时确定。 方法重载在编译时确定。

编译器根据可用的具体类型识别要调用的方法。

因此,c.print(c);是唯一与签名匹配的调用:Child.print(final Child child)

答案 2 :(得分:0)

我们只考虑两个对象,一个是P(Parent),第二个是C(Child)。现在P只能看到那些可以访问的方法和变量。在我们的例子中,它只能访问父类的print方法。因此,无论何时使用父对象调用print方法,它都只调用父类的print方法。

p.print(p);
p.print(c);
p.print(pc);

表示以上所有行都将调用父类的print方法,因为父级无法访问子级的方法。

对于像变量pc一样分配给父类的子对象,它将调用子类的覆盖方法,这是子类的父方法。

pc.print(p);
pc.print(c);
pc.print(pc);

现在对于使用子类C的方法,方法调用有两种可能性,一种是接受parent as an argument的打印方法,另一种是接受child as an argument。所以这一切都取决于你在方法调用中传递的参数。

c.print(p);
c.print(c);
c.print(pc);

在第一次和第三次调用中,您将父对象作为参数传递给方法,因此动态编译器根据传递的参数识别应该调用哪个方法,因此当父对象作为父对象传递时,将调用子类的父方法参数else将调用子类的子方法。