java-枚举的使用
2021-05-29 16:03
阅读:663
package com.company.suanfa;
/**
* 枚举写法和使用测试
* */
public enum Color {
FIRST("01", "一月"), SECOND("02", "二月");
// 通过key获取value
/**
* String key="01";
* System.out.println("key:"+key+",value="+Color.getValue(key));
*
* */
public static String getValue(String key) {
for (Color c : Color.values()) {
if (c.getKey() == key || c.getKey().equals(key)) {
return c.value;
}
}
return "sorry,未匹配到";
}
Color(String key, String value) {
this.key = key;
this.value = value;
}public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
private String value;
private String key;}
评论
亲,登录后才可以留言!