package enhance; /** * Created by coolkid on 2014/12/20 0020. */ public class EnumTest { public static void main(String[] args) { WeekDay weekDay = WeekDay.FRI; System.out.println(weekDay); System.out.println(weekDay.name()); System.out.println(weekDay.ordinal());//序号 System.out.println(weekDay.equals(WeekDay.FRI)); System.out.println(WeekDay.valueOf("SUN"));//字符串转枚举变量 //迭代枚举类型中的所有元素 WeekDay[] weekDays = WeekDay.values(); System.out.println("开始迭代"); for (WeekDay weekDay1:weekDays){ System.out.println(weekDay1.ordinal()+":"+weekDay1); } System.out.println("-------------------------"); TrafficLamp trafficLamp = TrafficLamp.GREEN; System.out.println(trafficLamp); System.out.println(trafficLamp.nextLamp()); } public enum WeekDay{ SUN(1),MON,TUE,WED,THI,FRI,SAT; //构造方法 private WeekDay(){} private WeekDay(int day){} } public enum TrafficLamp{ RED(30) { @Override public TrafficLamp nextLamp() { return GREEN; } }, GREEN(45) { @Override public TrafficLamp nextLamp() { return YELLOW; } }, YELLOW(5) { @Override public TrafficLamp nextLamp() { return RED; } }; //抽象方法 public abstract TrafficLamp nextLamp(); private int time; private TrafficLamp(int time){ this.time = time; } } }