二维枚举是java中的一个东西吗?即:
public enum Modules {
ATTACK(enum States{IDLE, NEAREST, NEARESTTOHQ;}),
MOVE(enum States{IDLE, NORTH, SOUTH, EAST, WEST;}),
SPAWN(enum States{IDLE, SIMPLESPAWN;});
}
答案 0 :(得分:0)
简而言之,没有。根据{{3}}:
枚举常量定义枚举类型的实例。
接下来,每个枚举常量,因为它是Enum的类型,不能包含新的嵌套成员声明。您可以将所有3个“子”枚举定义为Enum的嵌套成员,然后像以下一样访问它们:
Modules.AttackOptions.IDLE
通过相同的逻辑(并且JLS允许这样)Enum常量也可以包含抽象或默认方法的覆盖,并保持它们自己的“状态”,如下所示:
public enum Modules {
ATTACK(0, 1, 2) { public int getCost() { return 2;} },
MOVE (3, 4, 5) { }, // no method override
SPAWN (6, 7) { public int getCost() { return 1000;} };
private final int[] vars;
private Modules(int... vars) { this.vars = vars; }
public int getCost() { return 1; }
public int[] getVars() { return this.vars; }
public enum AttackOptions {
IDLE, NEAREST, SPAWN
}
}
答案 1 :(得分:0)
我在理解你要做的事情上有点麻烦,但可能会这样做吗?
enum States {
IDLE, NEAREST, NEARESTTOHQ,NORTH,SOUTH,EAST,WEST,IDLESPAWN
}
public enum Modules {
ATTACK(IDLE, NEAREST, NEARESTTOHQ),
MOVE(IDLE, NORTH, SOUTH, EAST, WEST),
SPAWN(IDLE, SIMPLESPAWN);
private States[] states;
public Modules(States... states) {
this.states=states;
}
}
(请注意,要获得我使用的语法,我相信您需要将状态放在其自己的文件中并包含导入状态。*在模块中)
答案 2 :(得分:0)
不是你想的意思,但这可以更直接地完成。
如果这些枚举能够保存States
数组,则可以在声明时设置其已知状态。
示例:
public enum Modules {
ATTACK(States.IDLE, States.NEAREST, States.NEARESTTOHQ),
MOVE(States.IDLE, States.NORTH, States.SOUTH, States.EAST, States.WEST),
SPAWN(States.IDLE, States.SIMPLESPAWN);
private final States[] knownStates;
private Modules(States... knownStates) {
this.knownStates = knownStates;
}
public States[] getKnownStates() {
return knownStates.clone();
}
}