简体   繁体   English

枚举中的Java重用代码

[英]java reuse code in enum

I had an enum like below: 我有一个如下的枚举:

public enum EOrderStatus  {

DELETE(0, "aaaa"),
CANCEL(1, "bbb"),
APPEND(2, "cccc");

private EOrderStatus(int value, String description)
{
    Description = description;
    Value = value;
}

//region common

public String getDescription() {
    return Description;
}
protected void setDescription(String description) {
    Description = description;
}
// 此枚举的描述,此值可以用于日志显示,或接口返回说明等
private String  Description;
public int getValue() {
    return Value;
}
protected void setValue(int value) {
    Value = value;
}
// int 值
private  int Value;

//endregion
}

and I have more than 100 enums like this. 我有100多个这样的枚举。 how can I reuse the code between region and endregion? 如何在区域和末端区域之间重用代码? ............................................................ ................................................... ..........

Not sure if it is good practice, but I'd say you can't really factor out the variables, but you could save yourself repeating the methods if you are willing to use reflection. 不知道这是否是一个好习惯,但是我想说您不能真正排除变量,但是如果您愿意使用反射,则可以省去重复使用这些方法的麻烦。

Given these Enums: 鉴于这些枚举:

enum Enum1 {
  DELETE(10, "aaaa1"),
  CANCEL(11, "bbb1"),
  APPEND(12, "cccc1");

  public final String description;
  public final int value;

  private Enum1(int value, String description) {
    this.description = description;
    this.value = value;
  }
}

enum Enum2  {
  DELETE(20, "aaaa2"),
  CANCEL(21, "bbb2"),
  APPEND(22, "cccc2");

  public final String description;
  public final int value;

  private Enum2(int value, String description) {
    this.description = description;
    this.value = value;
  }
}

You can create an utils class which reads the values using reflection: 您可以创建一个utils类,该类使用反射读取值:

class EnumUtils {

  public static String getDescription(Class enumClass, String enumName) {
    return (String) getFieldValue(enumClass, enumName, "description");
  }

  public static int getValue(Class enumClass, String enumName) {
    return (Integer) getFieldValue(enumClass, enumName, "value");
  }

  public static Object getFieldValue(Class enumClass, String enumName, String fieldName) {
    Object value = null;
    Enum e = Enum.valueOf(enumClass, enumName);
    try {
      Field descriptionField = e.getClass().getDeclaredField(fieldName);
      value = descriptionField.get(e);
    } catch(NoSuchFieldException | IllegalAccessException ex) { /* Handle that as you want */ }
    return value;
  }
}

Then you can use it this way: 然后,您可以通过以下方式使用它:

public static void main(String[] args)  {

  System.out.println(EnumUtils.getDescription(Enum1.class, "DELETE"));
  System.out.println(EnumUtils.getDescription(Enum2.class, "DELETE"));

  System.out.println(EnumUtils.getValue(Enum1.class, "CANCEL"));
  System.out.println(EnumUtils.getValue(Enum2.class, "APPEND"));

}

It outputs 输出

aaaa1
aaaa2
11
22

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM