千家信息网

Java枚举类在生产环境中怎么使用

发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章主要讲解了"Java枚举类在生产环境中怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java枚举类在生产环境中怎么使用"吧!使用大体分
千家信息网最后更新 2025年11月08日Java枚举类在生产环境中怎么使用

这篇文章主要讲解了"Java枚举类在生产环境中怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java枚举类在生产环境中怎么使用"吧!

    使用

    大体分为确定业务场景状态、定义枚举类、自定义查询方法、测试效果等几个部分。

    1、确定业务场景状态

    以我工作中实际的项目为例,智慧医院在挂号、门诊缴费时需要使用支付功能,我们目前实现了以下几种支付形式:微信小程序支付、微信H5支付、支付宝小程序支付、支付宝生活号支付、微信医保支付。
    那么,我们就可以针对这几种支付形式定义一个枚举类专门维护,今后需要新增、修改以及删除时,只需要修改这个枚举类即可。

    2、定义枚举类

    public enum PayTypeEnum {     WEI_XIN_MINI_APP("1", "wxma", "微信小程序支付"),     WEI_XIN_H5("2", "wxh6", "微信H5支付"),     ZFB_MINI_APP("3", "zfbma", "支付宝小程序支付"),     ZFB_H5("4", "zfbh6", "支付宝生活号支付"),     WEI_XIN_MEDICAL("5", "wxmedical", "微信医保支付");     private final String id;    private final String code;    private final String label;     PayTypeEnum(final String id, final String code, final String label) {        this.id = id;        this.code = code;        this.label = label;    }     public String getId() {        return id;    }     public String getCode() {        return code;    }     public String getLabel() {        return label;    } }

    3、自定义查询方法

    枚举类我们定义了id、code、label,那么我们使用过程中可能需要根据id获取枚举值、根据code获取枚举值(本人大部分时候都定义的这两个),甚至根据label获取枚举值,因此可以根据需要自定义自己的查询方法。

    /** * 根据id获取枚举对象 * @param id  */public static PayTypeEnum findById(String id) {    for (PayTypeEnum type : PayTypeEnum.values()) {        if (type.getId().equals(id))            return type;    }    return null;} /** * 根据code获取枚举对象 * @param code  */public static PayTypeEnum findByCode(String code) {    for (PayTypeEnum type : PayTypeEnum.values()) {        if (type.getCode().equals(code))            return type;    }    return null;}

    为了更完善,我们还可以再定义一个检查枚举类型的方法。

    /** * 检查支付类型是否有效 * @param id  */public static void check(String id) {    if (StringUtils.isEmpty(id)) {        throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");    }    for (PayTypeEnum type : PayTypeEnum.values()) {        if (type.getId().equals(id)) {            return;        }    }    throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");}

    最终代码如下:

    import com.web.rest.errors.BadRequestAlertException;import org.springframework.util.StringUtils; public enum PayTypeEnum {     WEI_XIN_MINI_APP("1", "wxma", "微信小程序支付"),     WEI_XIN_H5("2", "wxh6", "微信H5支付"),     ZFB_MINI_APP("3", "zfbma", "支付宝小程序支付"),     ZFB_H5("4", "zfbh6", "支付宝生活号支付"),     WEI_XIN_MEDICAL("5", "wxmedical", "微信医保支付");     private final String id;    private final String code;    private final String label;     PayTypeEnum(final String id, final String code, final String label) {        this.id = id;        this.code = code;        this.label = label;    }     public String getId() {        return id;    }     public String getCode() {        return code;    }     public String getLabel() {        return label;    }     /**     * 根据id获取枚举对象     * @param id      */    public static PayTypeEnum findById(String id) {        for (PayTypeEnum type : PayTypeEnum.values()) {            if (type.getId().equals(id))                return type;        }        return null;    }     /**     * 根据code获取枚举对象     * @param code     */    public static PayTypeEnum findByCode(String code) {        for (PayTypeEnum type : PayTypeEnum.values()) {            if (type.getCode().equals(code))                return type;        }        return null;    }     /**     * 检查支付类型是否有效     * @param id      */    public static void check(String id) {        if (StringUtils.isEmpty(id)) {            throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");        }        for (PayTypeEnum type : PayTypeEnum.values()) {            if (type.getId().equals(id)) {                return;            }        }        throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");    } }

    4、测试效果

    public static void main(String[] args) {    System.out.println("============= 获取枚举类的值 =============");   System.out.println("获取id:" + PayTypeEnum.WEI_XIN_MINI_APP.getId());   System.out.println("获取code:" + PayTypeEnum.WEI_XIN_MINI_APP.getCode());   System.out.println("获取label:" + PayTypeEnum.WEI_XIN_MINI_APP.getLabel());     System.out.println("============= 根据自定义的查询方法获取值 =============");   System.out.println("根据id获取枚举对象:" + PayTypeEnum.findById("3"));   System.out.println("根据code获取枚举对象:" + PayTypeEnum.findByCode("zfbma"));     System.out.println("============= 类型有效性检查 =============");   System.out.print("检查1:");   PayTypeEnum.check("1");   System.out.println();   System.out.print("检查2:");   PayTypeEnum.check("999");}

    打印如下:

    ============= 获取枚举类的值 =============
    获取id:1
    获取code:wxma
    获取label:微信小程序支付
    ============= 根据自定义的查询方法获取值 =============
    根据id获取枚举对象:ZFB_MINI_APP
    根据code获取枚举对象:ZFB_MINI_APP
    ============= 类型有效性检查 =============
    检查1:
    检查2:无效的支付类型

    Process finished with exit code 0

    感谢各位的阅读,以上就是"Java枚举类在生产环境中怎么使用"的内容了,经过本文的学习后,相信大家对Java枚举类在生产环境中怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

    支付 类型 检查 对象 程序 方法 查询 环境 生产 有效 医保 学习 生活 业务 内容 场景 形式 效果 有效性 状态 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网络安全厂商及产品 黑科技集中亮相互联网 各国网络安全产业排名 谷歌地球手机版数据库网址 天津统一软件开发服务参考价格 荒野行动连接不了服务器怎么办 网络安全个人自查报告表 企业服务器可以升级吗 公司的网络安全新闻稿 联想云服务器怎么样 卫生局网络安全领导小组 雄霸天下为什么用不了服务器 闵行区市场软件开发服务收费 后端和软件开发哪个好 软件开发是和程序员一样吗 不适合现代软件开发的模型 网络安全线上家长会反馈 数据库链表的作用 武清区国家网络安全宣传周 云服务器怎么强行关闭 浦东新区数据软件开发常见问题 娄底智能软件开发报价 软件开发领导级别 想学网络技术有什么书 没有网络安全广大人民群众 数据库前端制作实例 数据库山洞 oa和服务器时间不一致 奉贤区智能软件开发诚信合作 虚拟化软件nas存储 云服务器
    0