【发布时间】:2018-03-28 12:33:00
【问题描述】:
所以我正在做一些关于制作小游戏原型的课程。我有这些简单的类(以及其他一些不相关的类):
abstract class Weapon {
int damage;
int cost;
}
abstract class RangedWeapon extends Weapon {
int range;
int rounds;
}
class ExtraRounds extends Item{
int cost = 20;
int uses = 1;
void use(GameState state){
if (state.currentCharacter.weapon instanceof RangedWeapon){
state.currentCharacter.weapon.rounds += 10;
}
}
}
但是当我试图编译这个时,我得到了
Implementations.java:56: error: cannot find symbol
state.currentCharacter.weapon.rounds += 10;
^
symbol: variable rounds
location: variable weapon of type Weapon
我想要的只是 ExtraRounds 类来检查 weapon 是否属于 RangedWeapon 类并采取相应措施,但我不知道哪里出了问题。任何帮助表示赞赏
【问题讨论】:
-
武器类没有“回合”字段,因此您需要先将其施放为远程武器
标签: java inheritance abstract-class instanceof