JAVA编码(69)—— Java枚举实例

package com.pbs.rdm.common.securityEnum;

import com.pbs.rdm.common.util.CommonUtil;

/**
 * @author Created by xushuyi
 * @Description
 * @date 2018/12/27 13:23
 */
public enum TestEnum {

    /**
     * 定义一级
     */
    ENUM_TWO_TYPE1("001", "一级编码对应名称1"),
    ENUM_TWO_TYPE2("002", "一级编码对应名称2"),

    /**
     * 定义二级
     */
    ENUM_THREE_TYPE1("", "001", "二级编码对应名称1"),
    ENUM_THREE_TYPE2("", "002", "二级编码对应名称2"),;

    /**
     * 一级编码,名称构造器
     *
     * @param code 一级编码
     * @param name 名称
     */
    TestEnum(String code, String name) {
        this.code = code;
        this.name = name;
    }

    /**
     * 一二级编码,名称构造器
     *
     * @param code     编码
     * @param fullCode 全量编码
     * @param name     名称
     */
    TestEnum(String code, String fullCode, String name) {
        this.code = code;
        this.fullCode = fullCode;
        this.name = name;
    }

    /**
     * 编码
     */
    private String code;

    /**
     * 全量编码
     */
    private String fullCode;

    /**
     * 名称
     */
    private String name;

    public String getCode() {
        return code;
    }

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

    public String getFullCode() {
        return fullCode;
    }

    public void setFullCode(String fullCode) {
        this.fullCode = fullCode;
    }

    public String getName() {
        return name;
    }

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


    /**
     * 根据一级编码获取对应名称
     *
     * @param code 一级编码
     * @return 名称
     */
    public static String getNameByCode(String code) {
        if (!CommonUtil.isEmpty(code)) {
            for (TestEnum testEnum : TestEnum.values()) {
                if (!CommonUtil.isEmpty(testEnum.getCode())
                        && testEnum.getCode().equals(code)) {
                    return testEnum.getName();
                }
            }
        }
        return null;
    }

    /**
     * 根据二级编码获取对应名称
     *
     * @param fullCode 二级编码
     * @return 名称
     */
    public static String getNameByFullCode(String fullCode) {
        if (!CommonUtil.isEmpty(fullCode)) {
            for (TestEnum testEnum : TestEnum.values()) {
                if (!CommonUtil.isEmpty(testEnum.getFullCode())
                        && testEnum.getFullCode().equals(fullCode)) {
                    return testEnum.getName();
                }
            }
        }
        return null;
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(getNameByCode("001"));
        System.out.println(getNameByFullCode("001"));
    }
}

 

转载于:https://www.cnblogs.com/xushuyi/articles/10185182.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值