不能在Java中使用枚举(错误:找不到符号)
所以我有一个类文件只有我的枚举,看起来像这样
public class FactionNames { public 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 = 0 && end <= 10) { this.endurance = end; } else { this.endurance = 0; } }
因此,为了确保此类中的所有内容都能正常工作,我想在驱动程序中创建一些Dauntless对象,但我不断收到这些错误
D:\Documents\Google Drive\Homework\1331 Test.java:3: error: cannot find symbol Faction test; ^ symbol: class Faction location: class Test Test.java:4: error: cannot find symbol test = Faction.DAUNTLESS; ^ symbol: variable Faction location: class Test 2 errors
我正在使用看起来像这样的驱动程序。 我的语法有什么问题吗? 我无法弄清楚为什么我会收到这个错误。
public class Test { public static void main(String[] args) { Faction test; test = Faction.DAUNTLESS; Dauntless joe = new Dauntless("Joseph", "Hooper", 20, 5, 3, test); Dauntless vik = new Dauntless("Victoria", "Ward", 19, 6, 2, test); Dauntless winner; winner = joe.battle(vik); System.out.println(winner); } }
enum
类型Faction
嵌套在顶级类FactionNames
。
public class FactionNames { public enum Faction {AMITY, ABNIGATION, DAUNTLESS, EURIDITE, CANDOR}; }
如果要使用其简单名称,则需要导入它
import com.example.FactionNames.Faction;
或者,您可以使用其限定名称
FactionNames.Faction test = FactionNames.Faction.DAUNTLESS;;