编程思想_枚举

106 阅读1分钟

具体可参考文章:blog.csdn.net/wszcy199503…

枚举实战demo如下:

/**
 * 异常的信息通过枚举统一定义,方便定义管理
 * Created by LT on 2019/4/1.
 */
@Getter
public enum ResultEnum {

    SUCCESS(0, "成功"),
    SYSTEM_ERROR(1, "系统异常,请联系管理员"),
    PARAM_ERROR(2, "参数不正确"),

    OBJECT_NOT_EXIST(10, "更新的实体数据不存在")
    ;

    private Integer code;

    private String message;

    ResultEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
}

CommandType.java

    /**
     * 命令类型
     */
    public enum CommandType{
        /**
         * 新建
         */
        NEW("new"),
        /**
         * 启动
         */
        START("start"),
        /**
         * 列表
         */
        LIST("ls"),
        /**
         * 编码
         */
        private String code;

        /**
         * 构造函数
         * @param code
         */
        CommandType(String code){
            this.code = code;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }
    }