错误:

...
Caused by: java.lang.ExceptionInInitializerError
...
Caused by: java.lang.ClassCastException:
class com.evopulse.ds2150.TechTrees$BuildingTechTree
not an enum
at java.util.EnumSet.noneOf(Unknown Source)
at java.util.EnumSet.of(Unknown Source)
at com.evopulse.ds2150.TechTrees$BuildingTechTree.<clinit>(TechTrees.java:38)

这是我列举的摘录
public enum BuildingTechTree {
//Name                      SoftName                    Requirements
NONE                        ("NULL",                    null),

->下一行是崩溃的地方
BARRACKS                    ("Barracks",                EnumSet.of(NONE),
WALLS_SANDBAGS              ("Sandbag wall",            EnumSet.of(NONE),

POWERPLANT                  ("Power plant",             EnumSet.of(BARRACKS)),
GUARDTOWER                  ("Guard Tower",             EnumSet.of(BARRACKS));

将EnumSet.of(NONE)和EnumSet.of(BARRACKS)替换为null,可以进行初始化工作,但是由于缺少数据结构而破坏了我的代码……显然,但是我这样做是为了测试我的其余代码某种原因。

删除EnumSet.of(NONE)并仅替换为NONE,对BARRACKS相同,并更改所有相关的变量,构造函数和方法,这都不起作用...(甚至无法使用contains.all ,因为不适用于“我的更改变量” ...)

我使用第二种实现扩展了此示例:
https://gamedev.stackexchange.com/a/25652/48573

我还尝试通过逐字复制示例来跟踪步骤。添加
private static Set<BuildingTechTree> techsKnown;

techsKnown = (BuildingTechTree.BIODOME);
test = TechTrees.researchTech(techsKnown);

到另一个将用于测试初始化​​的类。不得不改变
public boolean researchTech(BuildingTechTree tech) {

静态

这导致了相同的“不是枚举”错误。我没有任何代表,要评论他的答案以指出初始化错误...

添加了两个当前答案的信息,因为这两个解决方案都会导致相同的新错误:
public class TechTrees {
private static Set<BuildingTechTree> techsKnown;

public TechTrees() {
    techsKnown = EnumSet.of(BuildingTechTree.NONE);       //Using this
    techsKnown = EnumSet.noneOf(BuildingTechTree.class);  //Or this
}

public static boolean researchTech(BuildingTechTree tech) {
    if (techsKnown.containsAll(tech.requirements)) {      //Causes null pointer
        return true;                                      //exception @ techsKnown
    }
    return false;
}

最佳答案

您的声明结构非常聪明,这很遗憾,无法使用。但是EnumSet显然需要首先对枚举进行完全初始化。它试图从枚举中获取常量数组,以便除其他外,它知道其内部位集需要多少空间。

这是一种解决方法。它使用一个助手方法,该方法首先创建一个普通集合(HashSet),然后在静态初始化块中迭代枚举常量,并用EnumSet替换所有集合。

public enum BuildingTechTree {
    // Named constants
    //Name                      SoftName                        Requirements
    NONE                        ("NULL",                        null),
    BARRACKS                    ("Barracks",                    setOf(NONE)),
    WALLS_SANDBAGS              ("Sandbag wall",                setOf(NONE)),
    POWERPLANT                  ("Power plant",                 setOf(BARRACKS)),
    GUARDTOWER                  ("Guard Tower",                 setOf(BARRACKS));

    private final String softName;
    private Set<BuildingTechTree> requirements;

    private BuildingTechTree(String softName, Set<BuildingTechTree> requirements) {
        this.softName = softName;
        this.requirements = requirements;
    }

    private static Set<BuildingTechTree> setOf(BuildingTechTree... values) {
        return new HashSet<>(Arrays.asList(values));
    }

    static {
        for (BuildingTechTree v : values()) {
            if (v.requirements == null) {
                v.requirements = EnumSet.noneOf(BuildingTechTree.class);
            } else {
                v.requirements = EnumSet.copyOf(v.requirements);
            }
        }
    }
}

关于java - Java:无法在枚举内使用EnumSet:初始化错误:Tech Research人才树示例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24584161/

10-10 03:37