有两个枚举。这些通过实例字段相互指向。但其中一个指向另一个列表。例如;
public enum EnumA {
A1(1, Arrays.asList(EnumB.B1, EnumB.B2)),
A2(2, Arrays.asList(EnumB.B3, EnumB.B4));
private int num;
private List<EnumB> enumBList;
private EnumA(int num, List<EnumB> enumBList) {
this.num = num;
this.enumBList = enumBList;
}
public List<EnumB> getEnumBList() {
return enumBList;
}
}
public enum EnumB {
B1(1, EnumA.A1),
B2(2, EnumA.A1),
B3(3, EnumA.A2),
B4(4, EnumA.A2);
private int num;
private EnumA enumA;
private EnumB(int num, EnumA enumA) {
this.num = num;
this.enumA = enumA;
}
public EnumA getEnumA() {
return enumA;
}
}
当我尝试获取&#34; List EnumBList&#34;时,它为空,我有NPE。
像:
EnumA.A1.getEnumBList();
EnumB.B1.getEnumA().getEnumBList();