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