Java Enumerated Types Error

不羁的心 提交于 2020-01-07 04:22:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!