java中根據int值來對應Enum
阿新 • • 發佈:2019-02-14
public enum XXXEnum{
Customer(1,"customer");
XXXEnum(int code,String value){
this.code = code;
this.value = value;
}
private String value;
private int code;
public String getValue() {
return value;
}
public int getCode() {
return code;
}
public static XXXEnum codeOf(int code){
for(XXXEnum xxxEnum : values()){
if(xxxEnum.getCode() == code){
return xxxEnum;
}
}
throw new RuntimeException("沒有找到對應的列舉");
}
}