映射枚举

映射枚举的定义

enum ColorEnum {
    YELLOW("YELLOW", "黄色"),
    WHITE("WHITE", "白色"),
    GREEN("GREEN", "绿色"),
    BLUE("BLUE", "蓝色"),
    RED("RED", "红色");

    private final String description;
    private final String code;

    // 每次实例都会调用一次  注意:新增一条数据就会用调用实例,一共五次
    ColorEnum(String code, String description) {
        System.out.println("编码:" + code + "描述:" + description);
        this.code = code;
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }
}

场景使用

System.out.println(ColorEnum.YELLOW.getCode());
System.out.println(ColorEnum.YELLOW.getDescription());
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容