首先创建一个枚举类
public enum GoodsState {
ONLINE("在售"),
OFFLINE("停售");
private String caption;
GoodsState(String caption) {
this.caption = caption;
}
public String getCaption(){
return this.caption;
}
}
通过类名及枚举值创建枚举对象
string className = "*****.GoodsState";
string value = "ONLINE";
try {
Class cls = Class.forName(className);
Method method = cls.getMethod("values");
Enum enums[] = (Enum[]) mm.invoke(null);
for (Enum ee : enums){
if (value.equals(ee.name())){
return ee;
}
}
} catch (Exception e){
System.out.println(e);
}