前言
在项目中有很多常量,我们都是使用枚举(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; // 不同的业务有不同的处理方式 }}
以上就是java 枚举使用方法的详细内容。