简单枚举实列

public enum EnumTest {


    ILLEGAL_TOKEN_TYPE("40002", "不合法的凭证类型"),
    ILLEGAL_MEDIA_TYPE("40004", "不合法的媒体文件类型"),
    SUCCESS("0", "请求成功");

    public static void main(String[] args) {

        System.out.println(getDescription("40002"));
    }

    String code;

    /**
     * 结果描述
     */
    String description;

    /**
     * 返回结果枚举构造方法
     *
     * @param code        结果码
     * @param description 结果描述
     */
    EnumTest(String code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * 通过code得到返回结果Description
     *
     * @param code 结果码
     * @return 结果枚举Description
     */
    public static String getDescription(String code) {
        EnumTest[] list = values();
        for (EnumTest resultType : list) {
            if (code.equals(resultType.getCode().toString())) {
                return resultType.getDescription();
            }
        }
        return null;
    }

    /**
     * 通过Description得到返回结果Code
     *
     * @param Description 状态
     * @return 结果枚举code
     */
    public static String getCode(String Description) {
        EnumTest[] list = values();
        for (EnumTest resultType : list) {
            if (Description.equals(resultType.getDescription().toString())) {
                return resultType.getCode();
            }
        }
        return null;
    }

    /**
     * 通过code得到返回结果对象
     *
     * @param code 结果码
     * @return 结果枚举对象
     */
    public static EnumTest get(String code) {
        EnumTest[] list = values();
        for (EnumTest resultType : list) {
            if (code.equals(resultType.getCode().toString())) {
                return resultType;
            }
        }
        return null;
    }

    /**
     * 获得结果码
     *
     * @return 结果码
     */
    public String getCode() {
        return code;
    }

    /**
     * 获得结果描述
     *
     * @return 结果描述
     */
    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return "ResultType{" +
                "code=" + code +
                ", description='" + description + '\'' +
                '}';
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40873693/article/details/116239538