java 枚举与构造函数中的枚举

发布于 2024-11-05 14:29:37 字数 554 浏览 0 评论 0原文

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

当使用这个时,我希望能够说这样的话:采用 MODAL_SETTINGS.EDIT_MODAL_WINDOW 并使用按钮 MODAL_BUTTONS.SAVE & 创建它。 MODAL_BUTTONS.取消。

这就是我现在所拥有的

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
}

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?

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.

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷途知返 2024-11-12 14:29:37

而不是这个:

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,而不是 EnumSet

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.

°如果伤别离去 2024-11-12 14:29:37

没问题:

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

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;
     }
}
久而酒知 2024-11-12 14:29:37

是的,看看这个:

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]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文