Java 在调用超级构造函数之前无法引用游戏

Java 在调用超级构造函数之前无法引用游戏,java,inheritance,constructor,super,Java,Inheritance,Constructor,Super,所以我有一个超级类Ghost,在构造函数中有另一个类Pacman的参数。现在为了创造4种不同类型的鬼魂(兰迪、傻瓜、特蕾西和苏,如果你喜欢吃豆人),它们有很多相似之处,我正在制作一个鬼魂的子类。在构造函数中,我从标题中得到了错误。 Ghost.class的一些代码: public class Ghost extends Actor { protected Pacman game; Randy randy; Ghost pinky; //totally unsure,

所以我有一个超级类Ghost,在构造函数中有另一个类Pacman的参数。现在为了创造4种不同类型的鬼魂(兰迪、傻瓜、特蕾西和苏,如果你喜欢吃豆人),它们有很多相似之处,我正在制作一个鬼魂的子类。在构造函数中,我从标题中得到了错误。
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;
以下是Randy的子类:

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的双重定义。“Randy”用作Ghost的成员,也用作类型。如果“Randy”是一种Ghost(每个Randy都是Ghost),则将
Ghost
抽象并使用一个子类。如果“Randy”是一个“MovementStrategy”,则创建一个新的
MovementStrategy
接口和它的
Randy
实现(然后在
Ghost
内部设置)

任何时候,一个幽灵都不应该有两个或更多的移动策略;因为,这样一来,一个幽灵就有了一个它没有使用的移动策略,这是粗糙的,完全不可取的

这也将避免需要执行“instanceof”来控制逻辑,因为有很多原因,您确实不应该执行此操作。如果
MovementStrategy
返回“下一个方向”,则依赖于多态性让“Randy”实例返回下一个方向。“instanceof”这很好地表明您正在将与数据类型相关的代码放在数据类型之外,这是一种反面向对象编程

一般来说,更好的做法是使用一种语言的设计选项,而不是违背该语言的设计选项,因此,除非有非常非常令人信服的理由,否则不要立即在Java中采用非面向对象技术

---编辑以将某些代码显示为插图---


为什么会被否决?当游戏本身没有传递给你的子类构造函数时,你不能用“game”调用超级构造函数。这不是唯一的问题。如果你传递了它,如果你实例化
Randy
类会发生什么?@Makoto-Huh?你说的“会发生什么”是什么意思?你只会做
new Randy(游戏)
…然后将创建一个新实例。不过,感觉这个问题并没有那么简单。可能还有更多问题。@user4425098堆栈溢出是另一个问题。也许可以问另一个问题?显示足够的代码来重现和回溯您得到的实际异常。为什么要用就其本身而言?Randy和其他幽灵的实例不应该是你的Pacman游戏类中的类字段吗?为什么你首先需要传递一个Pacman实例来创建你的演员?为什么你的
Ghost
类有实例变量
Randy
pinky
,这两种类型都是
Ghost
或子类型?t他似乎没有什么意义。你能给我们看<代码>演员< /COD>构造函数吗?还有什么是<代码> Pacman < /代码>?一个<代码>鬼魂< /代码>需要引用一个<代码> Pacman < /代码>?@约翰伯林格:是的,当我现在考虑这个问题的时候。e代码来说明这一点。代码并不完美,但它是一个起点。
如果“Randy”是一种幽灵(每个Randy都是幽灵)
这正是我试图创建的。“Randy”这里是一种移动策略,假设您的重影类型之间的唯一区别是它们的移动方式。如果不是这样,那么您仍然可以对特定重影进行子类化,并覆盖相关方法。
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());