您的位置:首页 > 编程语言 > Java开发

转载和积累系列 - JAVA枚举类型常用

2014-05-04 19:15 323 查看
/*最普通的枚举*/
public enum ColorSelect {
red, green, yellow, blue;
}

//带构造函数和属性的枚举类型
public enum TestEnum {
TEST_0("init", "100"), TEST_2("php", "200");

private String key;

private String value;

TestEnum(String key, String value) {
this.key = key;
this.value = value;
}

//可以通过key获取value的值
private final static Map<String, String> pool = new HashMap<String, String>();

static {
for (TestEnum each : TestEnum.values()) {
pool.put(each.key, each.value);
}
}

public static String getValue(String key) {
return pool.get(key);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: