public class EnumTest { public static void main(String[] args) { WeekDay1 weekDay = WeekDay1.MON; //System.out.println(weekDay.nextDay().toString()); WeekDay weekDay2 = WeekDay.SAT; System.out.println(weekDay2); //SAT System.out.println(weekDay2.ordinal()); //SAT排序 为6 System.out.println(weekDay2.valueOf("SUN").toString());//SUN 返回一个字符串表现形式 System.out.println(weekDay2.values().length);//values()表示一个数组 } //1.定义一个枚举类 public enum WeekDay{ SUN,MON,TUE,WED,THI,FRI,SAT; //所有的方法必须位于所有的元素之后 //枚举类的构造方法必须是私有的 private WeekDay(){ System.out.println("first"); } //如果要调用带有参数的构造器,可以这样 //SUN(1),MON,TUE,WED,THI,FRI,SAT; private WeekDay(int day){ System.out.println("second"); } } //交通灯 public enum TrafficLamp{ RED(30){ //红灯的下一个是绿灯 public TrafficLamp nextLamp(){ return GREEN; } }, GREEN(45){ //绿灯的下一个是黄灯 public TrafficLamp nextLamp(){ return YELLOW; } }, YELLOW(5){ //黄灯的下一个是红灯 public TrafficLamp nextLamp(){ return RED; } }; public abstract TrafficLamp nextLamp(); //指定的是交通灯亮的长度 private int time; private TrafficLamp(int time){ this.time = time; } }}打印结果为:secondsecondfirstfirstfirstfirstfirstSAT6SUN7相当于枚举类的每个元素都要被初始化构造