简体   繁体   English

子类方法不会覆盖父级中相同签名的方法

[英]Child class method not overriding the method of the same signature in parent

Why is my child class method not overriding the method of the same signature in its parent class? 为什么我的子类方法不会覆盖其父类中相同签名的方法?

Summary: 摘要:

I have a class PlayerPaddle that extends an abstract class Paddle , which extends the abstract class Actor (the context here is a pong game). 我有一个类PlayerPaddle ,它扩展了一个抽象类Paddle ,它扩展了抽象类Actor(这里的上下文是一个pong游戏)。 Paddle and PlayerPaddle both have a method with the same signature: PaddlePlayerPaddle都有一个具有相同签名的方法:

public void moveDown()

The intention is to have the PlayerPaddle moveDown method override that of Paddle . 目的是让PlayerPaddle moveDown方法覆盖Paddle方法。 The problem here is when I call this.moveDown() inside PlayerPaddle , only the moveDown method of Paddle is called. 这里的问题是当我在PlayerPaddle调用this.moveDown() ,只调用PaddlemoveDown方法。

The details: 细节:

Breakpoints set on moveDown() in Paddle are tripped, while breakpoints on moveDown() in PlayerPaddle never trip. Paddle中的moveDown()上设置的断点被触发,而在moveDown()上的PlayerPaddle永远不会跳闸。 I also tried @Override on the PlayerPaddle moveDown method, but it still calls the Paddle moveDown method. 我也试过@OverridePlayerPaddle moveDown的方法,但它仍然调用Paddle moveDown方法。

The context: 上下文:

The goal is for PlayerPaddle objects to move up/down at a different speed than other Paddle objects. 目标是让PlayerPaddle对象以与其他Paddle对象不同的速度向上/向下移动。 So the same problem occurs for the moveUp method as well. 因此, moveUp方法也会出现同样的问题。

Code below: 代码如下:

abstract public class Actor {
    public void releasedEvent(KeyEvent e) {
    }
}

abstract public class Paddle extends Actor {
     public void moveDown() { // this method is called
          body.setVel(0, Game.dX / 10.);   
     }
}


public class PlayerPaddle extends Paddle {
    @Override
    public void moveDown() { // this method never gets called
       body.setVel(0,  Game.dX );
    } 

    public void receiveEvent(KeyEvent e) {
       if (e.getKeyCode() == KeyEvent.VK_UP) {
          this.moveUp(); 
       } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
          this.moveDown(); // Want to call moveDown() inside PlayerPaddle
       }
    }

}

I am happy to provide clarification. 我很乐意提供澄清。

See the simple case, it works: 看看简单的情况,它有效:

public class Test{

  public static void main(String args[])
  {     
    new PlayerPaddle().receiveEvent();
  }
}
abstract class Actor {
  public void releasedEvent(KeyEvent e) {
  }
}
abstract class Paddle extends Actor {
  public void moveDown() { // this method is called
      System.out.println("paddle");
  }
}
class PlayerPaddle extends Paddle {
  @Override
  public void moveDown() { // this method never gets called
   System.out.println("playerpaddle");
  } 
  public void receiveEvent() {
     this.moveDown(); // Want to call moveDown() inside PlayerPaddle      
  }
}

It calls the PlayerPaddle one. 它调用PlayerPaddle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM