在构造函数中使用枚举的java枚举

时间:2023-01-26 14:53:01

Is it possible to let a enum in java take a set of enums as as argument? If yes, then how do I implement it?

是否可以让java中的枚举将一组枚举作为参数?如果是,那我该如何实现呢?

When using this I whant to be able to say something like: Take a MODAL_SETTINGS.EDIT_MODAL_WINDOW and create this with the buttons MODAL_BUTTONS.SAVE & MODAL_BUTTONS.CANCEL.

使用这个时,我想说的是:拿一个MODAL_SETTINGS.EDIT_MODAL_WINDOW并使用按钮MODAL_BUTTONS.SAVE和MODAL_BUTTONS.CANCEL创建它。

This is what I have as of now

这就是我现在所拥有的

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
}

3 个解决方案

#1


12  

Instead of this:

而不是这个:

NEW_MODAL_WINDOW(MODAL_BUTTONS.class),

I suspect you want this:

我怀疑你想要这个:

NEW_MODAL_WINDOW(EnumSet.allOf(MODAL_BUTTONS.class))

or

NEW_MODAL_WINDOW(EnumSet.of(MODAL_BUTTONS.SAVE, MODAL_BUTTONS.CANCEL))

(etc).

Otherwise you're just passing a Class<T>, not an EnumSet.

否则你只是传递一个Class ,而不是一个EnumSet。

#2


10  

No problem:

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

#3


2  

Yes, look at this:

是的,看看这个:

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

The output is:

输出是:

[SAVE, UPDATE]
[UPDATE, CANCEL]
[CANCEL, DELETE]

#1


12  

Instead of this:

而不是这个:

NEW_MODAL_WINDOW(MODAL_BUTTONS.class),

I suspect you want this:

我怀疑你想要这个:

NEW_MODAL_WINDOW(EnumSet.allOf(MODAL_BUTTONS.class))

or

NEW_MODAL_WINDOW(EnumSet.of(MODAL_BUTTONS.SAVE, MODAL_BUTTONS.CANCEL))

(etc).

Otherwise you're just passing a Class<T>, not an EnumSet.

否则你只是传递一个Class ,而不是一个EnumSet。

#2


10  

No problem:

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

#3


2  

Yes, look at this:

是的,看看这个:

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

The output is:

输出是:

[SAVE, UPDATE]
[UPDATE, CANCEL]
[CANCEL, DELETE]