无法在Java中使用枚举(错误:无法找到符号)

时间:2015-03-04 02:00:57

标签: java enums subclass

所以我有一个只有我的枚举的类文件,看起来像这样

public class FactionNames {
    public enum Faction {AMITY, ABNEGATION, DAUNTLESS, ERUDITE, 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;
        }
    }

因此,为了确保此类中的所有内容都能正常工作,我想在驱动程序中创建一些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);

    }
}

1 个答案:

答案 0 :(得分:4)

enum类型Faction嵌套在顶级班级FactionNames中。

public class FactionNames {
    public enum Faction {AMITY, ABNEGATION, DAUNTLESS, ERUDITE, CANDOR};
}

如果您想使用其简单名称,则需要将其导入

import com.example.FactionNames.Faction;

或者,您可以使用其限定名称

FactionNames.Faction test = FactionNames.Faction.DAUNTLESS;