在调用超级构造函数之前不能引用游戏

cannot reference game before super constructor has been called

所以我有一个超级 class Ghost,在构造函数中有一个来自另一个 class Pacman 的参数。现在为了创建 4 种不同类型的幽灵(兰迪、傻瓜、特雷西和苏,如果你喜欢 pacman)它们有很多相似之处,我正在制作一个子 class 幽灵。在构造函数中,我从标题中得到了错误。
Ghost.class的一些代码:

public class Ghost extends Actor
{
    protected Pacman game;
    Randy randy;
    Ghost pinky;    //totally unsure, but not a matter of question
    public Ghost(Pacman game)
    {
        this.game = game;

这是兰迪的子class:

private class Randy extends Ghost {

   Actor randy;

   public Randy(){
       super(game); //here is the error
       randy = new Actor();
       this.game = game;

这就是我这样做的原因:

public void act()
    {
        if (pinky instanceof Randy){
            moveRandom();     // Randy must move ONLY randomly unlike the other ghosts
    } 

    setSlowDown(2);

    code left out here

注意代码是零散的。
最后,我问这个问题,因为我还没有找到动态变量的解决方案。欢迎所有建议。

您需要将游戏作为参数传递给 Randy 的构造函数。

首先,我要摆脱兰迪的双重定义。 "Randy" 用作 Ghost 的成员,也用作类型。如果 "Randy" 是 Ghost 的一种(每个 Randy 都是 Ghost),将 Ghost 抽象化并使用子 class。如果 "Randy" 是一个 "MovementStrategy" 那么创建一个新的 MovementStrategy 接口和它的 Randy 实现(然后在 Ghost.[=17 内部设置它=]

任何时候一个幽灵都不应该有两个或更多的移动策略;因为,Ghost 有一个它没有使用的移动策略,这是 Cruft 并且完全不受欢迎。

这也将避免需要执行 "instanceof" 来控制您的逻辑,出于多种原因您确实不应该这样做。如果 MovementStrategy return 是 "next direction",则依靠多态性让 "Randy" 实例成为 return 的下一个方向。 "Instanceof" 很好地表明你将与数据类型相关的代码放在数据类型之外,这是反面向对象编程。

一般来说,最好使用语言的设计选择而不是反对语言的设计选择,所以在 Java 中不要立即接受非面向对象的技术,直到 非常 非常 非常有理由这样做。

--- 编辑以显示一些代码作为说明 ---

public Ghost extends Actor {

  private Game game;

  private MovementStrategy strategy;

  private Location location;

  public Ghost(Game game, MovementStrategy strategy) {
    this.game = game;
    this.movementStrategy = strategy;
  }

  // From Actor, I guess
  @Override
  public void act() {
    if (location == null) {
      location = game.addGhost(this);
    }
    move(movementStrategy.getDirection(game, location));
  }

  private void move(Direction direction) {
    location = game.move(this, direction);
  }

}

public interface MovementStrategy {

  public Direction getDirection(Game game, Location location);

}

public class Randy implements MovementStrategy {

  public Direction getDirection(Game game, Location location) {
    return selectRandom(game.getPermittedDirections());
  }

}

// constructing a Ghost with a Randy movement strategy

Game game = new Game();
Ghost ghost = new Ghost(game, new Randy());