Java 如何将枚举与分组和子分组层次结构/嵌套一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19680418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:20:50  来源:igfitidea点击:

How to use enum with grouping and subgrouping hierarchy/nesting

javaenumsnestedgroupinghierarchy

提问by user2763361

I have one enum'class' called Exampleas follows:

我有一个enum“类” Example,如下所示:

enum Example {
//enums belonging to group A:
   enumA1,
   enumA2,
   enumA3,
//enums belonging to group B:
   enumB1,
   enumB2,
   enumB3,
//enums belonging to group C:
   enumC1,
   enumC2,
   enumC3;
}

It's important for my project they all enums I work with belong to Example(since this is an argument in a constructor of a class).

这对我的项目很重要,我使用的所有枚举都属于Example(因为这是类的构造函数中的参数)。

How do I use enumhierarchy/nesting in order to achieve the following:

我如何使用enum层次结构/嵌套来实现以下目标:

  • A method that tests whether an enumis of group A, B or C. For example, something like Example.enumA1.isGroupBelonging(Group.A)or isGroupBelonging(Example.enumA1,Group.A)would be a public method that returns true.

  • Be able to do the same thing with sub-groups of group A, Band C. For example, group A might have subgroups a, band c. I then want a method that does something such as Example.enumA1.isSubGroupBelonging(SubGroup.a)which is a public boolean.

  • A way to do all this without needing to have some elaborate enumname that clogs up my code. For example, it would be nice to just be able to refer to Example.enumA1throughout my other classes without needing to refer to it using something like Example.enumA1(Group.A,SubGroup.a)or Example.enumA1.Group.A.SubGroup.a

  • 一种测试 a 是否属于enumA、B 或 C 组的方法。例如,类似于Example.enumA1.isGroupBelonging(Group.A)isGroupBelonging(Example.enumA1,Group.A)将是返回 true 的公共方法。

  • 能够做同样的事情组的子组ABC。例如,组 A 可能有子组abc。然后我想要一个方法来执行诸如Example.enumA1.isSubGroupBelonging(SubGroup.a)which is a 之类的事情public boolean

  • 一种无需复杂的enum名称即可完成所有这些操作的方法,这会阻塞我的代码。例如,能够Example.enumA1在我的其他类中引用而无需使用类似Example.enumA1(Group.A,SubGroup.a)Example.enumA1.Group.A.SubGroup.a

采纳答案by ???v?т?

I would use a very simple enum constructor which associates the corresponding group with the enum value:

我会使用一个非常简单的枚举构造函数,它将相应的组与枚举值相关联:

public enum Example {

    ENUM_A1 (Group.A),
    ENUM_A2 (Group.A),
    ENUM_A3 (Group.A),

    ENUM_B1 (Group.B),
    ENUM_B2 (Group.B),
    ENUM_B3 (Group.B),

    ENUM_C1 (Group.C),
    ENUM_C2 (Group.C),
    ENUM_C3 (Group.C);

    private Group group;

    Example(Group group) {
        this.group = group;
    }

    public boolean isInGroup(Group group) {
        return this.group == group;
    }

    public enum Group {
        A,
        B,
        C;
    }
}

Usage:

用法:

import static Example.*;
import Example.Group;
...

ENUM_A1.isInGroup(Group.A);  // true
ENUM_A1.isInGroup(Group.B);  // false

To do the subgroups you can do a similar kind of structure for Group as for Example, using Group(SubGroup ... subgroups)as a constructor and an EnumSet<SubGroup>to contain the subgroups.

要创建子组,您可以为 Group 执行与 Example 类似的结构,使用Group(SubGroup ... subgroups)构造函数和 anEnumSet<SubGroup>来包含子组。

回答by Wendel

You can use EnumSetto group various enums without creating a separate Enumclass:

您可以使用EnumSet对各种枚举进行分组,而无需创建单独的Enum类:

public enum Example {

    ENUM_A1, ENUM_A2, ENUM_A3,
    ENUM_B1, ENUM_B2, ENUM_B3,
    ENUM_C1, ENUM_C2, ENUM_C3;

    public static EnumSet<Example> groupA = EnumSet.of(ENUM_A1, ENUM_A2, ENUM_A3);
    public static EnumSet<Example> groupB = EnumSet.of(ENUM_B1, ENUM_B2, ENUM_B3);
    public static EnumSet<Example> groupC = EnumSet.of(ENUM_C1, ENUM_C2, ENUM_C3);

}   

public static void main(String[] args){
    if(Example.groupA.contains(Example.ENUM_A1)){
       System.out.println("Group A contains ENUM A1");
    }
}

回答by Christian

Small addition:

小补充:

I do like both answers from @ELEVATE and @Wendel. And it depends (as always) on the scenario when to use which solution. If each entry must be part of a single group I prefer ELEVATE's solution, because it forces me to add this single group. However if only some enum need to be grouped or they are not distinct I go with Wendel's solution, because it is more flexible (but more error prone)

我喜欢@ELEVATE 和@Wendel 的两个答案。这取决于(一如既往)使用哪种解决方案的场景。如果每个条目必须是单个组的一部分,我更喜欢 ELEVATE 的解决方案,因为它迫使我添加这个单个组。但是,如果只有一些枚举需要分组或者它们不明显,我会使用 Wendel 的解决方案,因为它更灵活(但更容易出错)