java 重用枚举中的代码
java reuse code in enum
我有一个如下所示的枚举:
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
}
我有 100 多个这样的枚举。
如何重用 region 和 endregion 之间的代码?
..................................................... ..........
不确定这是否是好的做法,但我想说你不能真正分解出变量,但如果你愿意使用反射,你可以避免重复这些方法。
鉴于这些枚举:
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;
}
}
您可以创建一个使用反射读取值的实用程序 class:
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;
}
}
那你可以这样用:
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"));
}
输出
aaaa1
aaaa2
11
22
我有一个如下所示的枚举:
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
}
我有 100 多个这样的枚举。 如何重用 region 和 endregion 之间的代码? ..................................................... ..........
不确定这是否是好的做法,但我想说你不能真正分解出变量,但如果你愿意使用反射,你可以避免重复这些方法。
鉴于这些枚举:
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;
}
}
您可以创建一个使用反射读取值的实用程序 class:
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;
}
}
那你可以这样用:
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"));
}
输出
aaaa1
aaaa2
11
22