【问题标题】:Subclass final method not being called未调用子类最终方法
【发布时间】:2017-05-22 17:32:53
【问题描述】:

我不明白这里发生了什么,但最后的方法

s.castable()

不会调用覆盖母类同名抽象方法的方法。

这是我尝试调用 s.castable() 的地方:

public void cast(String[] request) {
    System.out.println("cast called");
    if (this.session.getPlayer()==this.game.getTurnPlayer()) {
        System.out.println("first condition passed");
        Spell s = this.session.getPlayer().getCharacter().getSpells().get(Integer.valueOf(request[1]));
        ArrayList<String> usernames = new ArrayList();
        System.out.println("Now printing spell: "+s);

        for (int i = 6; i < request.length; i++) {
            usernames.add(request[i]);
        }
        System.out.println("username create.d");
        if (s.castable()) {                                   //HERE
            System.out.println("Second condition passed");
            s.cast(Integer.valueOf(request[1]), Integer.valueOf(request[2]),request[3].charAt(0), request[4].charAt(0), usernames);
            String str = "";
            for (String st : usernames) {
                str += st;
            }
            this.session.send("YOUSPELL "+request[1]+" "+request[2]+" "+request[3]+" "+request[4]+" "+str);
            System.out.println("Done");
        }
    }
}

这里是“Spell”母类:

public abstract class Spell {

private int manaCost;
private int coolDown;
private int range;
private Player player;

public abstract void cast(int x, int y, char mode1, char mode2,ArrayList<String> usernames);
public abstract Boolean castable();

//Then all getters and setters.
}

这是最后一个类“Velocity”:

public final class Velocity extends Spell {

private final int manaCost;
private final Player player;
private final int coolDown;
private final int coolDownTime;
private final int additionalMovement;
private final int spellRef;
private final ArrayList<String> usernames = new ArrayList();

public Velocity(Player p) {
    this.spellRef = 0;
    this.additionalMovement = 5;
    this.player = p;
    this.manaCost = 5;
    this.coolDownTime = 3;
    this.coolDown = 0;
    super.setCoolDown(coolDown);
    super.setManaCost(manaCost);
    super.setPlayer(p);
}

@Override
public final void cast(int x, int y, char mode1, char mode2,ArrayList<String> usernames) {
    System.out.println("Velocity casted.");
    player.setMovement(player.getMovement() + additionalMovement);
    setCoolDown(coolDownTime);
}

@Override
public final Boolean castable() {
    System.out.println(player.getMana());
    System.out.println(manaCost);
    System.out.println(getCoolDown());
    if (player.getMana() >= manaCost && getCoolDown() >= 0) {
        return true;
    }
    return false;
}

}

最后是控制台输出:

cast called first condition passed Now printing spell: model.haraka.be.Velocity@739bb60f username create.d.

如你所见,法术对象是已知的。

你能帮帮我吗?

谢谢

【问题讨论】:

  • 我不完全理解这里的问题。如果您指的是输出 model.haraka.be.Velocity@739bb60f ,那么您需要覆盖 toString() ,否则请澄清您的具体问题或添加其他详细信息以突出显示您需要的内容。
  • 代码太多:(请构造一个minimal test-case
  • @Aominè 不,System.out.println(s) 调用只是为了确保 s 对象是 instanceof Velocity。我尝试但不能做的是调用“s.castable()”方法
  • 您的s.castable() 只返回false。尝试调试它,看看是什么原因造成的。

标签: java oop methods abstract final


【解决方案1】:

这里唯一可能的问题是抽象类 Spell 的变量 s 不包含对 Velocity 对象的引用。

因此,velocity 类的 castable 方法永远不会被调用。

如果castable method 像许多人提到的那样返回 false people System.out.println() 声明必须打印,这不是 我认为的情况。

但要确定这是问题所在,请解释一下:

Spell s = this.session.getPlayer().getCharacter().getSpells().get(Integer.valueOf(request[1]));

下面的方法返回类型是什么?

  • getPlayer()
  • getSpells()
  • get(Integer.valueOf(request[1])

在评论部分提出/评论太多了,因此发布为答案。

【讨论】:

  • 我的错,正在调用 s.castable(),但 player.getMana()、manaCost 和 getCoolDown() 似乎没有返回任何内容。当我确定时,我会发布我的解决方案
猜你喜欢
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
相关资源
最近更新 更多