子类最终方法未被调用

时间:2017-05-22 17:32:53

标签: java oop methods abstract final

我不知道这里发生了什么,但是最后的方法

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");
        }
    }
}

这是&#34; Spell&#34; MotherClass:

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.
}

这是最后的课程&#34; Velocity&#34;:

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

你可以看到拼写对象已知。

你能帮助我吗?

谢谢

1 个答案:

答案 0 :(得分:0)

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

因此,不会调用velocity类的可转换方法。

  

如果许多人提到castable method返回false   必须打印人System.out.println()语句,而不是   我认为这个案例。

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

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

以下哪些方法返回类型?

  • getPlayer()
  • getSpells()
  • 得到(Integer.valueOf(请求[1])

在评论部分提出/评论太多,因此张贴作为答案。