java8 枚举



在java8中枚举理解为对象的枚举,需要定义对象的构造函数,属性以及对象的名称和属性值。
定义枚举

public enum ResponseCode {
    /*定义了7个对象*/
    CODE_200("200", "操作成功!!"),
    CODE_401("401", "未授权"),
    CODE_400("400", "请求错误"),
    CODE_403("403", "服务器拒绝请求"),
    CODE_405("405", "方法禁用"),
    CODE_409("409", "冲突"),
    CODE_500("500", "服务器内部错误");
    /*两个属性*/
    private String code;
    private String msg;
   /*构造函数*/
    private ResponseCode(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }
   /*属性的setter和getter*/
    public String getCode() {
        return this.code;
    }

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

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

使用

public <K> ResponseDTO<K> success() {
    ResponseDTO<K> responseDTO = new ResponseDTO(ResponseCode.CODE_200.getCode(), ResponseCode.CODE_200.getMsg());
    return responseDTO;
}


    原文作者:WangPing1223
    原文地址: https://blog.csdn.net/wangping1223/article/details/76828545
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞