Java枚举关联
我试图创建一个场景,其中通过Java枚举关联,java,enums,Java,Enums,我试图创建一个场景,其中通过enum class a中的enum常量,关联子enum 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 associ
enum class a
中的enum常量,关联子enum 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;
}
}
public enum A {
A1(B.B1),
A2(C.C2);
private final Enum<?> e;
private A(Enum<?> e) {
this.e = e;
}
static enum B {
B1,
B2;
}
static enum C {
C1(D.D1.getId()),
C2(D.D2.getId());
private String id;
private C(String id) {
this.id = id;
}
}
}
enum D {
D1("abc"),
D2("def");
private final String id;
private D(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
显然,这种语法不适用于OOTB,因为您不能以这种方式在Java中传递类。但是,有谁能建议一种我可以实现这一目标的方法吗
我希望使用它来验证客户端应用程序中的静态结构化分组。您不能使用构造函数
私有B(字符串strVal,enum associatedEnum)声明枚举a
。您可以在彼此内部声明其他枚举,但不能像这样。您不能使用构造函数private B(字符串strVal,enum associatedEnum)声明枚举a
。您可以在彼此内部声明其他枚举,但不能像这样。这应该可以满足您的需要。我已经包括了一个示例用法,其中将列出子枚举类型值
package a.b.c;
public class EnumsTest {
public enum A {
A1( B.class ),
A2( C.class );
private final Class<? extends Enum<?>> enumClazz;
A( final Class<? extends Enum<?>> enumClazz ) {
this.enumClazz = enumClazz;
}
public Enum<?>[] getSubEnumConstants() {
return enumClazz.getEnumConstants();
}
/**
* @param value
* @return Never null
* @throws IllegalArgumentException To be consistent with Enum.valueOf()
*/
public <T> Enum<?> valueOfSubEnum( final String value ) throws IllegalArgumentException {
for( Enum<?> enumInstance : getSubEnumConstants() ) {
if( enumInstance.name().equals( value ) ) {
return enumInstance;
}
}
throw new IllegalArgumentException( "valueOf for " + enumClazz.getName() + " could not be resoled with the value of " + value );
}
}
public enum B {
B1,
B2;
}
public enum C {
C1,
C2;
}
public static void main( String[] args ) {
for( A a : A.values() ) {
for( Enum<?> enumInstance : a.getSubEnumConstants() ) {
System.out.println( a.name() + ":" + enumInstance.name() );
}
}
Enum<?> valueOfSubEnum = A.A1.valueOfSubEnum( "B2" );
System.out.println( valueOfSubEnum.name() );
}
}
a.b.c包;
公共类枚举{
公共枚举A{
A1级(B级),
A2(C级);
私人最终课程>enumClazz;
A(最后一节课>enumClazz){
this.enumClazz=enumClazz;
}
公共枚举[]getSubEnumConstants(){
返回enumClazz.getEnumConstants();
}
/**
*@param值
*@return永不为空
*@throws IllegalArgumentException与Enum.valueOf()一致
*/
子枚举的公共枚举值(最终字符串值)引发IllegalArgumentException{
对于(枚举实例:getSubEnumConstants()){
if(enumInstance.name().equals(value)){
返回枚举实例;
}
}
抛出新的IllegalArgumentException(“无法将“+enumClazz.getName()+”的值解析为“+value”);
}
}
公共枚举B{
B1,
B2;
}
公共枚举C{
C1,
C2;
}
公共静态void main(字符串[]args){
对于(A:A.values()){
对于(枚举实例:a.getSubEnumConstants()){
System.out.println(a.name()+“:”+enumInstance.name());
}
}
子枚举的枚举值=A.A1.子枚举的值(“B2”);
System.out.println(valueOfSubEnum.name());
}
}
注意:如果您想将子类型锁定到一个特定的集合,您可以让它们实现一个接口。这应该是您想要的。我已经包括了一个示例用法,其中将列出子枚举类型值
package a.b.c;
public class EnumsTest {
public enum A {
A1( B.class ),
A2( C.class );
private final Class<? extends Enum<?>> enumClazz;
A( final Class<? extends Enum<?>> enumClazz ) {
this.enumClazz = enumClazz;
}
public Enum<?>[] getSubEnumConstants() {
return enumClazz.getEnumConstants();
}
/**
* @param value
* @return Never null
* @throws IllegalArgumentException To be consistent with Enum.valueOf()
*/
public <T> Enum<?> valueOfSubEnum( final String value ) throws IllegalArgumentException {
for( Enum<?> enumInstance : getSubEnumConstants() ) {
if( enumInstance.name().equals( value ) ) {
return enumInstance;
}
}
throw new IllegalArgumentException( "valueOf for " + enumClazz.getName() + " could not be resoled with the value of " + value );
}
}
public enum B {
B1,
B2;
}
public enum C {
C1,
C2;
}
public static void main( String[] args ) {
for( A a : A.values() ) {
for( Enum<?> enumInstance : a.getSubEnumConstants() ) {
System.out.println( a.name() + ":" + enumInstance.name() );
}
}
Enum<?> valueOfSubEnum = A.A1.valueOfSubEnum( "B2" );
System.out.println( valueOfSubEnum.name() );
}
}
a.b.c包;
公共类枚举{
公共枚举A{
A1级(B级),
A2(C级);
私人最终课程>enumClazz;
A(最后一节课>enumClazz){
this.enumClazz=enumClazz;
}
公共枚举[]getSubEnumConstants(){
返回enumClazz.getEnumConstants();
}
/**
*@param值
*@return永不为空
*@throws IllegalArgumentException与Enum.valueOf()一致
*/
子枚举的公共枚举值(最终字符串值)引发IllegalArgumentException{
对于(枚举实例:getSubEnumConstants()){
if(enumInstance.name().equals(value)){
返回枚举实例;
}
}
抛出新的IllegalArgumentException(“无法将“+enumClazz.getName()+”的值解析为“+value”);
}
}
公共枚举B{
B1,
B2;
}
公共枚举C{
C1,
C2;
}
公共静态void main(字符串[]args){
对于(A:A.values()){
对于(枚举实例:a.getSubEnumConstants()){
System.out.println(a.name()+“:”+enumInstance.name());
}
}
子枚举的枚举值=A.A1.子枚举的值(“B2”);
System.out.println(valueOfSubEnum.name());
}
}
注意:如果要将子类型锁定到特定的集合,可以让它们实现一个接口。这对我来说很有效,但我可能错过了您试图实现的目标:
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;
}
}
public enum A {
A1(B.B1),
A2(C.C2);
private final Enum<?> e;
private A(Enum<?> e) {
this.e = e;
}
static enum B {
B1,
B2;
}
static enum C {
C1(D.D1.getId()),
C2(D.D2.getId());
private String id;
private C(String id) {
this.id = id;
}
}
}
enum D {
D1("abc"),
D2("def");
private final String id;
private D(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
公共枚举A{
A1(B.B1),
A2(C.C2);
私有最终枚举e;
私人A(枚举e){
这个。e=e;
}
静态枚举B{
B1,
B2;
}
静态枚举C{
C1(D.D1.getId()),
C2(D.D2.getId());
私有字符串id;
专用C(字符串id){
this.id=id;
}
}
}
枚举D{
D1(“abc”),
D2(“def”);
私有最终字符串id;
私有D(字符串id){
this.id=id;
}
公共字符串getId(){
返回id;
}
}
这对我来说很有效,但我可能错过了您想要实现的目标:
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;
}
}
public enum A {
A1(B.B1),
A2(C.C2);
private final Enum<?> e;
private A(Enum<?> e) {
this.e = e;
}
static enum B {
B1,
B2;
}
static enum C {
C1(D.D1.getId()),
C2(D.D2.getId());
private String id;
private C(String id) {
this.id = id;
}
}
}
enum D {
D1("abc"),
D2("def");
private final String id;
private D(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
公共枚举A{
A1(B.B1),
A2(C.C2);
私有最终枚举e;
私人A(枚举e){
这个。e=e;
}
静态枚举B{
B1,
B2;
}
静态枚举C{
C1(D.D1.getId()),
C2(D.D2.getId());
私有字符串id;
专用C(字符串id){
this.id=id;
}
}
}
枚举D{
D1(“abc”),
D2(“def”);
私有最终字符串id;
私有D(字符串id){
this.id=id;
}
公共字符串getId(){
返回id;
}
}
嘿,谢谢,我真的意识到了这一点。有什么建议吗?为每个枚举创建一个单独的文件。。然后进行如下配置:问题:是否需要创建树结构?是吗?嘿,谢谢,我早就知道了。有什么建议吗?为每个枚举创建一个单独的文件。。然后进行如下配置:问题:是否需要创建树结构?是吗?忘了加。。这是输出。。。A1:B1 A1:B2 A2:C1 A2:C2这与我想做的非常接近。。。最终,我希望能够对子枚举中的值进行变量化,如下所示:a.A1.getEnumClazz().valueOf(“B1”)。但是,.valueOf()
方法无法访问,即使它扩展了枚举。。。有没有办法解决这个问题?我已经更新了这个示例,它有一个粗略的valueOf like方法。您可能希望查看Enum.valueOf()方法,以确保它与其行为一致。忘记添加。。这是t