在构造函数中带有enum的Java枚举



是否可以让java中的枚举作为参数?如果是,那么我如何实现它?

当使用这个时,我希望能够这样说:取一个MODAL_SETTINGS。EDIT_MODAL_WINDOW并使用按钮MODAL_BUTTONS创建它。保存,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
}

而不是:

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

没问题:

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;
     }
}

是的,看这个:

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]

相关内容

  • 没有找到相关文章

最新更新