自定义枚举:
使用enum定义枚举类:
定义有参枚举 :
1 public enum TestEnum { 2 3 SPRING("春天","春风又绿江南岸"), 4 SUMMER("夏天","映日荷花别样红"), 5 AUTUMN("秋天","秋水共长天一色"), 6 WINTER("冬天","窗含西岭千秋雪"); 7 private final String seasonName; 8 private final String seasonDesc; 9 private TestEnum(String seasonName, String seasonDesc) { 10 this.seasonName = seasonName; 11 this.seasonDesc = seasonDesc; 12 } 13 public String getSeasonName() { 14 return seasonName; 15 } 16 public String getSeasonDesc() { 17 return seasonDesc; 18 } 19 20 21 22 }
test:
1 public static void main(String[] args) { 2 String f=TestEnum.SPRING.getSeasonName(); 3 if (f=="春天") { 4 System.out.println("HELLO:"+f); 5 } 6 7 }
枚举参一:
1 public enum TestEnum { 2 3 SPRING("春天"), 4 SUMMER("夏天"), 5 AUTUMN("秋天"), 6 WINTER("冬天"); 7 private final String seasonName; 8 9 private TestEnum(String seasonName) { 10 this.seasonName = seasonName; 11 } 12 public String getSeasonName() { 13 return seasonName; 14 } 15 16 }
test同上。。。。
枚举无参:
1 public enum TestEnum { 2 3 SPRING, 4 SUMMER, 5 AUTUMN, 6 WINTER; 7 8 }