java枚举类的使用
直接上代码
/**
* @description 响应结果枚举类
*/
public enum ResponseResult {
NORMAL("200", "返回正常"),
LOGINERROR("201", "点赞"),
ROOMNUMBEREEOR("202", "加"),
SERVICEERRORS("203", "关注"),
private String key;
private String value;
ResponseResult(String key, String value) {
this.key = key;
this.value = value;
}
//调用该方法通过key,来获取value值
public static String getValue(String key) {
for (ResponseResult responseResult : ResponseResult.values()) {
if (key.equals(responseResult.key)) {
return responseResult.value;
}
}
return null;
}
}
测试
public class test {
public static void main(String[] args) {
String value = ResponseResult.getValue("201");
System.out.println("通过枚举类中的key值,来获取value:"+value);
}
}