Java枚举类型错误 [英] Java Enumerated Types Error

查看:181
本文介绍了Java枚举类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 枚举阵营{
AMITY,ABNIGATION,DAUNTLESS,EURIDITE,CANDOR
};

我试图通过使用驱动程序来检查我的子类中的所有内容是否工作。我的子类中的构造函数看起来像这样。

  public Dauntless(String f,String l,int a,int ag,int end,Faction d){
超级(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;
无耻的乔=新的无畏(约瑟夫,胡珀,20,5,3,这个);
无畏Vik =新无畏(维多利亚,病房,19,6,2,这个);
无耻的赢家
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); 
无敌vik =新无畏(维多利亚,病房,19,6,2,this_faction);


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?

解决方案

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

这篇关于Java枚举类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆