我正在尝试创建一个场景,其中枚举常量 inenum class A
具有关联的 subenum class B
并enum class C
包含它们自己的常量。中的常数enum class B
和常数的enum class C
组子集来自enum class D
。以下是我想要实现的基本目标:
enum A {
CONST_1 ("const_1", B), // B is the associated enum
CONST_2 ("const_2", C); // C in the associated enum
private final String strVal;
private final Enum associatedEnum;
private A (String strVal, Enum associatedEnum) {
this.strVal = strVal;
this.associatedEnum = associatedEnum;
}
public Enum getAssociatedEnum() {
return this.associatedEnum;
}
public String toString() {
return this.strVal;
}
// Associated Enum contained subset of grouped constants
enum B {
CONST_3 (D.CONST_7.toString()),
CONST_4 (D.CONST_8.toString());
private final String strVal;
private B (String strVal) {
this.strVal = strVal;
}
public String toString() {
return this.strVal;
}
}
// Associated Enum contained subset of grouped constants
enum C {
CONST_5 (D.CONST_9.toString()),
CONST_6 (D.CONST_10.toString());
private final String strVal;
private C (String strVal) {
this.strVal = strVal;
}
public String toString() {
return this.strVal;
}
}
}
// Separate Enum containing all ungrouped constants
enum D {
CONST_7 ("const_7"),
CONST_8 ("const_8");
CONST_9 ("const_9"),
CONST_10 ("const_10");
private final String strVal;
private D (String strVal) {
this.strVal = strVal;
}
public String toString() {
return this.strVal;
}
}
显然,这种语法在 OOTB 中不起作用,因为您不能以这种方式在 Java 中传递类。但是任何人都可以提出一种我可以实现这一目标的方法吗?
我希望用它来验证客户端应用程序中的静态结构化分组。