1. 程式人生 > >java中根據int值來對應Enum

java中根據int值來對應Enum

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("沒有找到對應的列舉");
    }
}