java基础—-枚举类

JDK5.0之前

自定义枚举类

public class test{
    public static void main(String[] args){
        Season spring = Season.SPRING;
        System.out.println(spring);
    }  
}
class Season{
    //声明Season对象属性
    private final String seasonName;
    private final String seasonDes;

    //私有化类构造器,并给对象赋值
    private Season(String seasonName,String seasonDes){
        this.seasonName = seasonName;
        this.seasonDes = seasonDes;
    }

    public static final Season SPRING = new Season("春天","万物复苏");
    public static final Season SUMMER = new Season("夏天","夏日炎炎");
    public static final Season AUTUMN = new Season("秋天","秋高气爽");
    public static final Season WINTER = new Season("冬天","冰天雪地");

    public String toString(){
        return seasonName;
    }
}

JDK5.0之后

enum Season{
    SPRING("春天","万物复苏"),
    SUMMER("夏天","夏日炎炎"),
    AUTUMN("秋天","秋高气爽"),
    WINTER("冬天","冰天雪地");

    private final String seasonName;
    private final String seasonDes;

    //私有化类构造器,并给对象赋值
    private Season(String seasonName,String seasonDes){
        this.seasonName = seasonName;
        this.seasonDes = seasonDes;
    }
}

您可能还喜欢...

发表评论

电子邮件地址不会被公开。 必填项已用*标注