level
从实例访问字段的正确方法是Minotaur
什么?得到错误for (int i =0 ; i < ((Monster) this).level ; i++)
是Cannot cast from Player to Monster
。
package player;
import monsters.*;
public class Player extends GameCharacter {
public void performAttackOn(GameCharacter who){
if (who instanceof Monster ){
for (int i =0 ; i < ((Monster) this).level ; i++) { // <<
}
public static class Minotaur extends Monster{ // << nested class which is being created and there is need to access it's level
public Minotaur () {
type = "Minotaur";
}
}
}
package monsters;
public class Monster extends GameCharacter{
public int level = 1;
}
package monsters;
public abstract class GameCharacter{
public static class Minotaur extends Monster{
public Minotaur(){
type = "Minotaur";
hitPoints = 30;
weapon = new Weapon.Sword();
}
}
}
Minotaur
应该在继承自的某些方法中扩展monsters.GameCharacter
和monsters.Monster
覆盖Player.player
monsters.GameCharacter