Java枚举类型错误

时间:2015-03-04 00:30:27

标签: java enums subclass

所以我有一个基类,我用这个代码块定义一个枚举变量。

    enum Faction {
            AMITY, ABNIGATION, DAUNTLESS, EURIDITE, CANDOR
        };

我正在尝试通过使用驱动程序来测试我的子类中的所有内容是否都能正常工作。我的子类中的构造函数看起来像这样。

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;
        }
    }

我的司机看起来像这样

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;不是声明。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,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);