问题
so I have a base class where I define an enum variable with this block of code.
enum Faction {
AMITY, ABNIGATION, DAUNTLESS, EURIDITE, CANDOR
};
And I'm trying to test to see if everything in my subclass works by using a driver. My constructor in my subclass looks like this.
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;
}
}
And my driver looks like this
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);
}
It keeps saying that Faction this = Faction.DAUNTLESS;
is not a statement. Can somebody help me out here?
回答1:
As mentioned in the comments, this
is a keyword in Java, used for things like:
this.faction;
You can't use keywords as variable names. Just change the variable name:
Faction this_faction = Faction.DAUNTLESS;
Then, of course, you need to change references to the variable:
Dauntless joe = new Dauntless("Joseph", "Hooper", 20, 5, 3, this_faction);
Dauntless vik = new Dauntless("Victoria", "Ward", 19, 6, 2, this_faction);
来源:https://stackoverflow.com/questions/28844637/java-enumerated-types-error