我想创建一个场景,其中由enum class A
中的枚举常量具有关联的子enum class B
和enum class C
包含它们自己的常量。 enum class B
和enum class C
中的常量从enum class D
组常量的子集。下面是一个基本上就是我想实现:Java枚举协会
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;
}
}
显然,这句法不起作用开箱即用的,因为你无法通过Java中的类这种方式。但任何人都可以提出一种我可以实现这一目标的方法吗?
我希望能用它来验证客户端应用程序中的静态结构化分组。
http://stackoverflow.com/questions/221892/java-extendable-enumeration?rq=1 – 2012-07-30 23:23:47