Java 枚举类型错误
Java Enumerated Types Error
所以我有一个基础 class,我用这段代码定义了一个枚举变量。
enum Faction {
AMITY, ABNIGATION, DAUNTLESS, EURIDITE, CANDOR
};
我正在尝试使用 driver 来测试我的子 class 中的所有内容是否正常工作。我的 subclass 中的构造函数看起来像这样。
public Dauntless(String f, String l, int a, int ag, int end, Faction d) {
super(f, l, a, d);
if (ag >= 0 && ag <= 10) {
this.agility = ag;
} else {
this.agility = 0;
}
if (end >= 0 && end <= 10) {
this.endurance = end;
} else {
this.endurance = 0;
}
}
我的driver看起来像这样
public class Test {
public static void main(String[] args) {
Faction this = Faction.DAUNTLESS;
Dauntless joe = new Dauntless("Joseph", "Hooper", 20, 5, 3, this);
Dauntless vik = new Dauntless("Victoria", "Ward", 19, 6, 2, this);
Dauntless winner;
winner = joe.battle(vik);
System.out.println(winner);
}
一直在说Faction this = Faction.DAUNTLESS;
不是一个说法。有人可以帮我吗?
如评论中所述,this
是 Java 中的关键字,用于以下内容:
this.faction;
您不能使用关键字作为变量名。只需更改变量名称:
Faction this_faction = Faction.DAUNTLESS;
然后,当然,您需要更改对变量的引用:
Dauntless joe = new Dauntless("Joseph", "Hooper", 20, 5, 3, this_faction);
Dauntless vik = new Dauntless("Victoria", "Ward", 19, 6, 2, this_faction);
所以我有一个基础 class,我用这段代码定义了一个枚举变量。
enum Faction {
AMITY, ABNIGATION, DAUNTLESS, EURIDITE, CANDOR
};
我正在尝试使用 driver 来测试我的子 class 中的所有内容是否正常工作。我的 subclass 中的构造函数看起来像这样。
public Dauntless(String f, String l, int a, int ag, int end, Faction d) {
super(f, l, a, d);
if (ag >= 0 && ag <= 10) {
this.agility = ag;
} else {
this.agility = 0;
}
if (end >= 0 && end <= 10) {
this.endurance = end;
} else {
this.endurance = 0;
}
}
我的driver看起来像这样
public class Test {
public static void main(String[] args) {
Faction this = Faction.DAUNTLESS;
Dauntless joe = new Dauntless("Joseph", "Hooper", 20, 5, 3, this);
Dauntless vik = new Dauntless("Victoria", "Ward", 19, 6, 2, this);
Dauntless winner;
winner = joe.battle(vik);
System.out.println(winner);
}
一直在说Faction this = Faction.DAUNTLESS;
不是一个说法。有人可以帮我吗?
如评论中所述,this
是 Java 中的关键字,用于以下内容:
this.faction;
您不能使用关键字作为变量名。只需更改变量名称:
Faction this_faction = Faction.DAUNTLESS;
然后,当然,您需要更改对变量的引用:
Dauntless joe = new Dauntless("Joseph", "Hooper", 20, 5, 3, this_faction);
Dauntless vik = new Dauntless("Victoria", "Ward", 19, 6, 2, this_faction);