1、最古老的
//未处理
public static final Integer PROCESS_STATUS_UNTREATED = 0;
//已接收
public static final Integer PROCESS_STATUS_ACCPECTED = 1;
//已处理
public static final Integer PROCESS_STATUS_PROCESSED = 2;
2、改进版的
public static final class PROCESS_STATUS{
//未处理
public static final Integer UNTREATED = 0;
//已接收
public static final Integer ACCPECTED = 1;
//已处理
public static final Integer PROCESSED = 2;
}
3、枚举版(好处显而易见,既可以获取code又能获取汉字),以后定义常量采用此方法
/**
* 常量的枚举定义接口
* @author yangzhilong
* @param <T>
*
*/
public interface BaseConstant<T> {
T getCode();
String getDesc();
}
/**
* 操作类型
* @author yangzhilong
*
*/
public enum OPERATION_TYPE implements BaseConstant<String>{
ADD("add", "新增"),
UPDATE("update", "修改"),
DELETE("delete", "删除"); private String code;
private String desc;
OPERATION_TYPE(String key, String value) {
this.code = key;
this.desc = value;
} @Override
public String getCode() {
return code;
} @Override
public String getDesc() {
return desc;
}
}
调用方式:
//获取编码
PROCESS_STATUS.UNTREATED.getCode();
//获取描述
PROCESS_STATUS.UNTREATED.getDes();
根据枚举类型和编码获取code的描述:
public class ConstantUtils { /**
* 根据枚举的code获取desc
* @param <T>
* @param clazz
* @param codeValue
* @return
*/
public static <T extends Enum<T> & BaseConstant<T>> String getEnumValue(Class<T> clazz ,
Object codeValue) {
String result = null;
T[] enums = clazz.getEnumConstants();
for(T obj : enums){
if(obj.getCode().equals(codeValue)){
result = obj.getDesc();
break;
}
}
return result;
} /**
* 根据枚举的code获取整个枚举对象
* @param <T>
* @param clazz
* @param codeValue
* @return
*/
public static <T extends Enum<T> & BaseConstant<T>> BaseConstant<T> getEnum(Class<T> clazz ,
Object codeValue) {
BaseConstant<T> result = null;
T[] enums = clazz.getEnumConstants();
for(T obj : enums){
if(obj.getCode().equals(codeValue)){
result = obj;
break;
}
}
return result;
}
}