自己写的枚举类
package com.yanan.java; /** * Created by zhangyanana on 2017/2/28. */ public class TestSeason { public static void main(String[] args) { Season season=Season.AUTUMN; System.out.println(season.getSeasonName()+" "+season.getSeasonDesc()); } } //自定义枚举类(类似于单例模式) class Season { //1.提供类的属性,声明为private final-----外部无法修改属性且属性值一旦被赋值就不可改变。 private final String seasonName; private final String seasonDesc; //2.私有化构造器,让外部无法创建这个类的对象 private Season(String seasonName, String seasonDesc) { this.seasonName = seasonName; this.seasonDesc = seasonDesc; } //3.通过公用的方法来调用属性 public String getSeasonDesc() { return seasonDesc; } public String getSeasonName() { return seasonName; } //4.在类的内部创建类的对象(枚举类对象),设置为public static final public static final Season SPRING = new Season("spring", "春暖花开"); public static final Season SUMMER = new Season("summer", "夏日炎炎"); public static final Season AUTUMN = new Season("autumn", "秋高气爽"); public static final Season WINTER = new Season("winter", "冬雷滚滚"); }
执行结果: