I have one enum
'class' called Example
as follows:
我有一个名为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 enum
hierarchy/nesting in order to achieve the following:
如何使用枚举层次结构/嵌套来实现以下目的:
-
A method that tests whether an
enum
is of group A, B or C. For example, something likeExample.enumA1.isGroupBelonging(Group.A)
orisGroupBelonging(Example.enumA1,Group.A)
would be a public method that returns true.测试枚举是否属于组A,B或C的方法。例如,像Example.enumA1.isGroupBelonging(Group.A)或isGroupBelonging(Example.enumA1,Group.A)这样的公式方法将返回true 。
-
Be able to do the same thing with sub-groups of group
A
,B
andC
. For example, group A might have subgroupsa
,b
andc
. I then want a method that does something such asExample.enumA1.isSubGroupBelonging(SubGroup.a)
which is apublic boolean
.能够对组A,B和C的子组执行相同的操作。例如,组A可能具有子组a,b和c。然后我想要一个方法,例如Example.enumA1.isSubGroupBelonging(SubGroup.a),它是一个公共布尔值。
-
A way to do all this without needing to have some elaborate
enum
name that clogs up my code. For example, it would be nice to just be able to refer toExample.enumA1
throughout my other classes without needing to refer to it using something likeExample.enumA1(Group.A,SubGroup.a)
orExample.enumA1.Group.A.SubGroup.a
一种方法来完成所有这些,而不需要一些精心设计的枚举名称堵塞我的代码。例如,能够在我的其他类中引用Example.enumA1而不需要使用像Example.enumA1(Group.A,SubGroup.a)或Example.enumA1.Group.A这样的引用它会很好。 .SubGroup.a
2 个解决方案
#1
39
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执行类似的结构,例如,使用Group(子组...子组)作为构造函数,使用EnumSet
#2
25
You can use EnumSet
to group various enums without creating a separate Enum
class:
您可以使用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");
}
}
#1
39
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执行类似的结构,例如,使用Group(子组...子组)作为构造函数,使用EnumSet
#2
25
You can use EnumSet
to group various enums without creating a separate Enum
class:
您可以使用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");
}
}