Java枚举类案例
package com.loit.common.enums;标签:incrementKey,code,Java,String,CodeEnum,Demo,枚举,return,public From: https://www.cnblogs.com/lfh-blog/p/16737905.html
/**
* @author zhaokz
* @date 2022/9/7
* 编码枚举
*/
public enum CodeEnum {
/**
* 性别
*/
SEX0("0", "保密"),
SEX1("1", "男"),
SEX2("2", "女"),
/**
* 是否
*/
NO("0", "否"),
IS("1", "是"),
;
private final String code;
private final String incrementKey;
CodeEnum(String code, String incrementKey) {
this.code = code;
this.incrementKey = incrementKey;
}
public String getCode() {
return code;
}
public String getIncrementKey() {
return incrementKey;
}
/**
* 根据code获取enum
*/
public static CodeEnum getCodeEnumByCode(String code) {
CodeEnum result = null;
for (CodeEnum s : values()) {
if (s.getCode().equals(code)) {
result = s;
break;
}
}
return result;
}
/**
* 通过code获取incrementKey
*/
public static String getIncrementKeyByCode(String code) {
CodeEnum[] codeEnums = values(); //获取所有枚举集合
for (CodeEnum codeEnum : codeEnums) {
if (codeEnum.getCode().equals(code)) {
return codeEnum.getIncrementKey();
}
}
return null;
}
/**
* 通过incrementKey获取code
*/
public static String getCodeByIncrementKey(String incrementKey) {
CodeEnum[] codeEnums = values(); //获取所有枚举集合
for (CodeEnum codeEnum : codeEnums) {
if (codeEnum.getIncrementKey().equals(incrementKey)) {
return codeEnum.getCode();
}
}
return null;
}
}