3

是否可以让 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
}
4

3 回答 3

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))

(ETC)。

否则,您只是通过 a Class<T>,而不是EnumSet.

于 2011-05-06T10:26:16.503 回答
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;
     }
}
于 2011-05-06T10:47:05.620 回答
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]
于 2011-05-06T10:57:11.317 回答