Java - 静态和动态类型 - "这打印什么?"

Rya*_* Yu 3 java oop inheritance static dynamic

所以我在理解类中显示的这个例子时遇到了一些麻烦 - 它应该说明Java中静态和动态类型之间的微妙之处.

public class Piece {

    public static void main (String[] args) {
        Piece p2 = new Knight();
        Knight p1 = new Knight();
        p1.capture(p2); // call 1; "Knight is bored" is supposed to be printed
        p2.capture(p1); // call 2; "knight is bored" is supposed to be printed
    }

    public void capture () {
        System.out.println("Capturing");
    }

    public void capture (Piece p) {
        System.out.println("I'm bored")
    }

public class Knight extends Piece {

    public void capture (Piece p) {
        System.out.println("Knight is bored");
    }

    public void capture (Knight k) {
        System.out.println("TOO SLOW BUDDY");
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我对两次调用时会发生什么的理解:

致电1:p1.capture(第2页)

从p1调用捕获方法.通过动态类型查找,它看到p1的动态类型是Knight.所以它看起来在Knight子类中.p2作为参数传入.要查看在Knight子类中调用哪个捕获方法,它会检查p2 的静态类型,即片段.因此,印有"骑士无聊".这是正确的输出,但我的推理是正确的吗?

调用2:p2.capture(p1)

使用相同的推理,从p2调用捕获方法.通过动态类型查找,它看到p2的动态类型是Knight.所以它看起来在Knight子类中.p1作为参数传入.要查看要调用的捕获方法,它会查看p1的静态类型,即Knight.因此,打印出"TOO SLOW BUDDY".显然,我的推理是错误的,因为那不是真正印刷的.任何方向?

谢谢!

Sim*_*ser 5

在第二个调用中,您只能调用Piece类的方法或其子类中的相同方法.这就是为什么它会调用捕获(Piece p)而不是捕获(Knight k).后者特定于骑士类.

例如,当我们有"List a = new Arraylist();"时,你只能调用在List中声明的方法,而不是在ArrayList中调用类似的方法.