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;
        }
    }

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章