我正在使用Java instanceof ,但它似乎无法正常工作。
我有三个 java类,它们扩展了一个Hero类 Hero.java 类:
public abstract class Hero {
protected int health;
public Hero() {
}
}
其他三个类:
public class Archer extends Hero {
public Archer() {
}
}
public class Mage extends Hero {
public Mage() {
}
}
public class Warrior extends Hero {
public Warrior() {
}
}
我有这个主要类 WelcomeScreen.java
public class WelcomeScreen {
private Archer archer;
private Mage mage;
private Warrior warrior;
private Hero hero;
public WelcomeScreen() {
// choose a hero (archer/mage/warrior)
hero = archer;
new Game(hero);
}
public static void main(String args[]) {
new WelcomeScreen();
}
}
实例化 Game.java 类
public class Game {
public Game(Hero chosenHero) {
if (chosenHero instanceof Mage) {
System.out.println("you selected mage");
} else if (chosenHero instanceof Archer) {
System.out.println("you selected archer");
} else if (chosenHero instanceof Warrior) {
System.out.println("you selected warrior");
} else {
System.out.println("you selected NOTHING");
}
}
}
在 Game.java 中,代码用于检查selectedHero是否为 Archer.java , Warrior.java 的对象,或者 Mage.java ,但我的结果是"您选择了NOTHING" 。为什么 instanceof 无法检查我是否已将其分配给WelcomeScreen中的 Archer.java ?
答案 0 :(得分:2)
因为你的常量是null
。当你说,
private Archer archer;
等同于
private Archer archer = null;
此外,您已为每个实例创建了三个字段。我想你想做点什么
private static final Hero archer = new Archer();
private static final Hero mage = new Mage();
private static final Hero warrior = new Warrior();
答案 1 :(得分:2)
替代解决方案:摆脱instanceof,因为它暗示了一种易碎的刚性设计。而是尝试使用其他更符合OOP的解决方案,例如继承,或者如果复杂,则使用访问者设计模式。
例如,简单的继承结构可能类似于:
public class WelcomeScreen {
public WelcomeScreen() {
// choose a hero (archer/mage/warrior)
Hero hero = new Archer();
new Game(hero);
}
public static void main(String args[]) {
new WelcomeScreen();
}
}
abstract class Hero {
protected int health;
// other shared fields such as String name,...
public Hero() {
}
public abstract String getType();
public int getHealth() {
return health;
}
}
class Archer extends Hero {
public static final String TYPE = "Archer";
public Archer() {
}
@Override
public String getType() {
return TYPE;
}
}
class Mage extends Hero {
public static final String TYPE = "Mage";
public Mage() {
}
@Override
public String getType() {
return TYPE;
}
}
class Warrior extends Hero {
public static final String TYPE = "Warrier";
public Warrior() {
}
@Override
public String getType() {
return TYPE;
}
}
class Game {
private Hero hero;
public Game(Hero chosenHero) {
this.hero = chosenHero;
System.out.println("You selected a hero of type " + hero.getType());
}
}