java枚举//内部静态类

内部静态类

public class Testconstans {

    public static final String test="测试";
    /** 奖惩记录 */
    public static class RewardPunishment {
        public static final String REWARD = "奖励";
        public static final String PUNISHMENT = "惩罚";
    }
    /** 奖惩记录 */
    public static class RewardPunishment1 {
        public static final String REWARD1 = "奖励1";
        public static final String PUNISHMENT1 = "惩罚1";
    }
}

枚举方式

public enum ServiceCode {
    SYSTEM_NORMAL(200, "系统正常"),
    //异常 返回码范围 400 -419 业务无关异常
    INVALID_PARAM(401,"无效参数"),
    //服务端异常
    SERVICE_ERROR(500, "服务异常");
    private int code;
    private String desc;
    ServiceCode(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
}

取值

//内部静态类取值
System.out.println(Testconstans.RewardPunishment.PUNISHMENT);
        System.out.println(Testconstans.test);
        //获取枚举里面值
        System.out.println(ServiceCode.SERVICE_ERROR.getDesc());
        //获取枚举
        ServiceCode[] values = ServiceCode.values();
        for (ServiceCode value:values
             ) {
            System.out.println(value);
        }

猜你喜欢

转载自blog.csdn.net/WSFLOVE/article/details/89842564