版权声明
1. 本站文章和资源均来自互联网收集和整理,本站不承担任何责任及版权问题。
2. 相关版权归作者及其公司所有,仅供学习研究用途,请勿用于商业目的。
3. 若侵犯您的版权,请发邮件至webmaster@ishare1.cn联系我们,我们确认后将立即删除。
前言

在项目中有很多常量,我们都是使用枚举(enum)来处理,下面我就和大家分享一个比较通用的代码
枚举
/**
* 描述: 常量类型
* /
public enum ClientType {
SYSTEM(0, "后台管理"),
EDUCATION(1, "教育系统"),
GOVERNMENT(2, "政府系统");
private Integer value;
private String text;
ClientType(Integer value, String text) {
this.value = value;
this.text = text;
}
public Integer getValue() {
return this.value;
}
public String getText() {
return this.text;
}
/**
*根据值找相对应的中文
*/
public static String getTextByValue(Integer value) {
return Arrays.stream(values()) // java8新特性 -- stream流
.filter(x -> x.getValue().equals(value))
.map(ClientType::getText)
.findFirst().orElse("");
}
}
枚举在java代码使用比较简单
在应用层的使用方法
// 获取类型相对应的数值
Integer type = ClientType .SYSTEM.getValue();
// 获取中文
Intger code = 1; // 初始化
for (ClientType value : ClientType.values()) {
if (type.value== code) {
return type; // 不同的业务有不同的处理方式
}
}
爱分享




