【发布时间】: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