java 枚举的优雅定义
新建枚举
public enum CommissionStatusEnum {
ONGOING(10, "进行中"),
FINISHED(20, "已完成"),
CLOSED(30, "已关闭");
private Integer key;
private String value;
CommissionStatusEnum(Integer key, String value) {
this.key = key;
this.value = value;
}
public static String getValue(Integer key) {
for (CommissionStatusEnum c : CommissionStatusEnum.values()) {
if (c.getKey().equals(key)) {
return c.value;
}
}
return null;
}
public static String getName(Integer key) {
for (CommissionStatusEnum c : CommissionStatusEnum.values()) {
if (c.getKey().equals(key)) {
return c.name();
}
}
return "";
}
public static CommissionStatusEnum getEnum(Integer key) {
for (CommissionStatusEnum c : CommissionStatusEnum.values()) {
if (c.getKey().equals(key)) {
return c;
}
}
return null;
}
public static boolean isExist(Integer key) {
for (CommissionStatusEnum c : CommissionStatusEnum.values()) {
if (c.getKey().equals(key)) {
return true;
}
}
return false;
}
public Integer getKey() {
return key;
}
public void setKey(Integer key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
获取枚举的key
CommissionStatusEnum.FINISHED.getKey();
获取枚举的value
CommissionStatusEnum.FINISHED.getValue();
获取枚举的名称
CommissionStatusEnum.getName(CommissionStatusEnum.CLOSED.getKey());
CommissionStatusEnum.getEnum(CommissionStatusEnum.CLOSED.getKey());