来自尚硅谷枚举类视频
/**
* 枚举类:类的对象只有有限个、确定的。
* 当需要一组常量时,强烈建议使用枚举类
* 如果枚举类只有 一个对象时,则可以作为单例模式的实现方式。
*/
class Season {
private final String seasonName;
//构造器定义为私有,防止在此类外进行实例创建
private Season(String seasonName) {
this.seasonName = seasonName;
}
//定义确定个数的对象
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 getSeasonName() {
return seasonName;
}
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
'}';
}
}
public class EnumeDe001 {
public static void main(String[] args) {
System.out.println(Season.SPRING);
String spring = Season.SPRING.getSeasonName();
System.out.println(spring);
}
}