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("没有找到对应的枚举");
    }
}

猜你喜欢

转载自blog.csdn.net/july_young/article/details/81270324