1 package enhance; 2 3 /** 4 * Created by coolkid on 2014/12/20 0020. 5 */ 6 public class EnumTest { 7 public static void main(String[] args) { 8 WeekDay weekDay = WeekDay.FRI; 9 System.out.println(weekDay); 10 System.out.println(weekDay.name()); 11 System.out.println(weekDay.ordinal());//序号 12 System.out.println(weekDay.equals(WeekDay.FRI)); 13 System.out.println(WeekDay.valueOf("SUN"));//字符串转枚举变量 14 //迭代枚举类型中的所有元素 15 WeekDay[] weekDays = WeekDay.values(); 16 System.out.println("开始迭代"); 17 for (WeekDay weekDay1:weekDays){ 18 System.out.println(weekDay1.ordinal()+":"+weekDay1); 19 } 20 System.out.println("-------------------------"); 21 TrafficLamp trafficLamp = TrafficLamp.GREEN; 22 System.out.println(trafficLamp); 23 System.out.println(trafficLamp.nextLamp()); 24 } 25 26 public enum WeekDay{ 27 SUN(1),MON,TUE,WED,THI,FRI,SAT; 28 29 //构造方法 30 private WeekDay(){} 31 private WeekDay(int day){} 32 } 33 34 public enum TrafficLamp{ 35 RED(30) { 36 @Override 37 public TrafficLamp nextLamp() { 38 return GREEN; 39 } 40 }, 41 GREEN(45) { 42 @Override 43 public TrafficLamp nextLamp() { 44 return YELLOW; 45 } 46 }, 47 YELLOW(5) { 48 @Override 49 public TrafficLamp nextLamp() { 50 return RED; 51 } 52 }; 53 //抽象方法 54 public abstract TrafficLamp nextLamp(); 55 private int time; 56 private TrafficLamp(int time){ 57 this.time = time; 58 } 59 60 } 61 }