是否可以让java中的枚举将一组枚举作为参数?如果是,那我该如何实施呢?
使用这个时,我想说的是:拿一个MODAL_SETTINGS.EDIT_MODAL_WINDOW并使用MODAL_BUTTONS.SAVE&按钮创建它。 MODAL_BUTTONS.CANCEL。
这就是我现在所拥有的
public enum MODAL_SETTINGS {
NEW_MODAL_WINDOW(MODAL_BUTTONS.class),
EDIT_MODAL_WINDOW(MODAL_BUTTONS.class),
DELETE_MODAL_WINDOW(MODAL_BUTTONS.class);
private EnumSet buttons;
private MODAL_SETTINGS(EnumSet<MODAL_BUTTONS> buttons){
}
}
public enum MODAL_BUTTONS {
SAVE, UPDATE, CANCEL, DELETE
}
答案 0 :(得分:12)
而不是:
NEW_MODAL_WINDOW(MODAL_BUTTONS.class),
我怀疑你想要这个:
NEW_MODAL_WINDOW(EnumSet.allOf(MODAL_BUTTONS.class))
或
NEW_MODAL_WINDOW(EnumSet.of(MODAL_BUTTONS.SAVE, MODAL_BUTTONS.CANCEL))
(等)。
否则,您只是传递Class<T>
,而不是EnumSet
。
答案 1 :(得分:10)
没问题:
enum Color {YELLOW, BLUE, GREEN}
enum Environment {
SUN(Color.YELLOW), OCEAN(Color.BLUE), TREE(Color.GREEN);
private Color color;
private Environment(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
}
答案 2 :(得分:2)
是的,看看这个:
import java.util.EnumSet;
public class EnumTest {
public static enum MODAL_BUTTONS {
SAVE, UPDATE, CANCEL, DELETE
}
public static enum MODAL_SETTINGS {
NEW_MODAL_WINDOW(EnumSet.of(MODAL_BUTTONS.SAVE, MODAL_BUTTONS.UPDATE)),
EDIT_MODAL_WINDOW(EnumSet.of(MODAL_BUTTONS.UPDATE, MODAL_BUTTONS.CANCEL)),
DELETE_MODAL_WINDOW(EnumSet.of(MODAL_BUTTONS.CANCEL, MODAL_BUTTONS.DELETE));
private EnumSet<MODAL_BUTTONS> buttons;
MODAL_SETTINGS(EnumSet<MODAL_BUTTONS> buttons){
this.buttons = buttons;
}
EnumSet<MODAL_BUTTONS> getBtns() {
return this.buttons;
}
}
public static void main(String... args) {
System.out.println(MODAL_SETTINGS.NEW_MODAL_WINDOW.getBtns());
System.out.println(MODAL_SETTINGS.EDIT_MODAL_WINDOW.getBtns());
System.out.println(MODAL_SETTINGS.DELETE_MODAL_WINDOW.getBtns());
}
}
输出结果为:
[SAVE, UPDATE]
[UPDATE, CANCEL]
[CANCEL, DELETE]