0

我想对动态多态有一些清晰的了解。当子类中的方法被覆盖和重载时,我无法弄清楚方法调用。

这是父类:

家长班:

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

    }

}

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

4

3 回答 3

1

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

那么,让我们看看在您的每个方法调用中会发生什么......

我们将忽略 using 调用,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 "
于 2015-10-23T04:31:09.483 回答
0

这个问题看起来像是方法重载和覆盖之间的差异的副本

覆盖(多态性)和重载之间存在核心区别。

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

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

所以 forc.print(c);是唯一与签名匹配的调用:Child.print(final Child child)

于 2015-10-23T04:13:06.807 回答
0

让我们只考虑两个对象,一个是P(Parent),第二个是C(Child)。现在 P 只能看到它可以访问的那些方法和变量。在我们的例子中,它只能访问父类的打印方法。所以每当你使用父对象调用打印方法时,它只会调用父类的打印方法。

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

意味着上述所有行将调用父类的打印方法,因为父类无法访问子类的方法。

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

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

现在对于使用子类 C 的方法,方法调用有两种可能,一种是 print 方法,它接受parent as an argument,第二种是 accept child as an argument。因此,这完全取决于您在方法调用中传递的参数。

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

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

于 2015-10-23T04:16:41.503 回答