枚举在开发中的应用

public enum AttachmentType {

    account("人员"),monitorEnv("设施环境评测评价"),monitorUse("临床使用效果评价"),monitorSafe("质量安全检测评价"),
    equipment("设备合同文件");


    private String description;

    AttachmentType(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public static AttachmentType getAttechmentTypeByDesc(String desc) {
        if (!StringUtil.hasText(desc))
            return null;
        for (AttachmentType t : values()) {
            if (t.getDescription().equals(desc))
                return t;
        }
        return null;
    }

    public static String getCnName(String enName) {//得到汉语名称
        if (!StringUtil.hasText(enName))
            return "";
        for (AttachmentType e : values()) {
            if (enName.equals(e.name()))
                return e.getDescription();
        }
        return "";
    }
}

 在开发中,无经验者常常会把系统用到的一些常量在控制器中定义“死”,这样就不利于后期的维护和拓展。解决这一问题就要用到java的枚举,枚举可以很好的解决在控制器中写死的问题,我们可以把系统中所有用到的常量都放在已定义的枚举中,这样取数据和后期维护以及系统的拓展都有很大的方便。枚举很好用,要善用枚举。

原文地址:https://www.cnblogs.com/blog411032/p/5799619.html