Java之枚举举例

时间:2021-07-25 01:56:23
 package enumdemo;

 /**
* 枚举类
*/ public enum MAPPER { // 实例
ELEMENT_NAME("mapper"),
ATTRIBUTE_RESOURCE("resource"); private String content; MAPPER(String content) {
this.content = content;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
}
}
 package enumdemo;

 /**
* 枚举测试
*/
public class Test {
public static void main(String[] args) {
String content = "";
content = MAPPER.ATTRIBUTE_RESOURCE.getContent();
System.out.println(content); String content2 = MAPPER.valueOf("ELEMENT_NAME").getContent();
System.out.println(content2); for (MAPPER mapper : MAPPER.values()) {
System.out.println(mapper.getContent());
}
}
}

运行结果:

resource
mapper
mapper
resource