enum通用写法

public static enum AmisChartType {
        @JsonProperty("line")
        LINE("line", "折线图"),
        @JsonProperty("bar")
        BAR("bar", "柱状图"),
        @JsonProperty("pie")
        PIE("pie", "饼状图");

        private String code;
        private String name;

        AmisChartType(String code, String name) {
            this.code = code;
            this.name = name;
        }

        public String getCode() {
            return this.code;
        }

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

        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public static AmisChartType of(String code) {
            AmisChartType[] items = values();
            for (AmisChartType item : items) {
                if (item.getCode().equals(code)) {
                    return item;
                }
            }
            throw new RuntimeException("不合法的AmisChartType枚举code");
        }

        public static String getNameByCode(String code) {
            return of(code).getName();
        }

        @Override
        public String toString(){
            return this.code;
        }
    }

  

posted @ 2022-11-10 17:41  zslm___  阅读(50)  评论(0)    收藏  举报