Java之枚举举例
1 package enumdemo; 2 3 /** 4 * 枚举类 5 */ 6 7 public enum MAPPER { 8 9 // 实例 10 ELEMENT_NAME("mapper"), 11 ATTRIBUTE_RESOURCE("resource"); 12 13 private String content; 14 15 MAPPER(String content) { 16 this.content = content; 17 } 18 19 public String getContent() { 20 return content; 21 } 22 23 public void setContent(String content) { 24 this.content = content; 25 } 26 }
1 package enumdemo; 2 3 /** 4 * 枚举测试 5 */ 6 public class Test { 7 public static void main(String[] args) { 8 String content = ""; 9 content = MAPPER.ATTRIBUTE_RESOURCE.getContent(); 10 System.out.println(content); 11 12 String content2 = MAPPER.valueOf("ELEMENT_NAME").getContent(); 13 System.out.println(content2); 14 15 for (MAPPER mapper : MAPPER.values()) { 16 System.out.println(mapper.getContent()); 17 } 18 } 19 }
运行结果:
resource
mapper
mapper
resource