其他分享
首页 > 其他分享> > Enum类及方法

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);//冬天
    }

  

 

标签:SeasonEnum,name,Enum,private,String,类及,方法,public,temperature
来源: https://www.cnblogs.com/ixtao/p/16603423.html