说每对对方的第一个定义都有错误.
public enum Baz{ yin(yang), //Cannot reference a field before it is defined yang(yin), good(evil), //Cannot reference a field before it is defined evil(good); public final Baz opposite; Baz(Baz opposite){ this.opposite = opposite; } }
我想要完成的是能够使用Baz.something.opposite来获得Baz.something的相反对象.这有可能的解决方法吗?也许在这个例子中定义了yin和good之前的阳性和坏的空占位符?
你可以尝试类似的东西:
public enum Baz{ yin("yang"), yang("yin"), good("evil"), evil("good"); private String opposite; Baz(String opposite){ this.opposite = opposite; } public Baz getOpposite(){ return Baz.valueOf(opposite); } }
然后将其引用为
Baz.something.getOpposite()
这应该通过它的字符串表示查找枚举值来完成你想要做的事情.我不认为你能用它来处理Baz的递归引用.
翻译自:https://stackoverflow.com/questions/12592286/java-enum-cannot-reference-a-field-before-it-is-defined