Enum类及方法
//定义的枚举类默认继承于java.lang.Enum类 public enum SeasonEnum { //1.提供当前枚举类的对象,多个对象之间用","隔开,末尾对象";"结束 SPRING("春天",20), SUMMER("夏天",35), AUTUMN("秋天",25), WINTER("夏天",-5); //2.声明Season对象的属性: private final修饰 private final String name; private final int temperature; //3.私有化类的构造器,并给对象属性赋值 SeasonEnum(String name, int temperature) { this.name = name; this.temperature = temperature; } public String getName() { return name; } public int getTemperature() { return temperature; }}
@Test public void test5(){ SeasonEnum[] values = SeasonEnum.values(); for (SeasonEnum value : values) { System.out.print(value+" ");//SPRING SUMMER AUTUMN WINTER } String winter = SeasonEnum.valueOf("WINTER").getName(); System.out.println(winter);//冬天 }