枚举
public abstract class WeekDay { private WeekDay(){} private final static WeekDay MON=new WeekDay(){ @Override public WeekDay nextday() { // TODO Auto-generated method stub return SUN; } }; final static WeekDay SUN=new WeekDay(){ @Override public WeekDay nextday() { // TODO Auto-generated method stub return MON; } }; public abstract WeekDay nextday(); // public WeekDay nextDay() // { // if(this==SUN) // { // return Mon; // }else{ // return SUN; // } // } public String toString() { return this==SUN?"sun":"mon"; } }
public class numerDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub WeekDay weekDay=WeekDay.SUN; System.out.println(weekDay.nextday()); ==========================================================//调用上面的WeekDay WeekDay1 weekDay2=WeekDay1.FRI; System.out.println(weekDay2.name()); System.out.println(weekDay2.ordinal()); System.out.println(weekDay2.getClass()); System.out.println(WeekDay1.valueOf("SUN")); System.out.println(WeekDay1.values().length); } public enum WeekDay1{ SUN,MON,TUE,WED,THI,FRI,SAT } }
枚举的抽象的应用 package numDemo; public class EnumTest { public static void main(String[] args) { WeekDay weekDay2=WeekDay.FRI; } public enum WeekDay{ SUN(1),MON,TUE,WEN,THT,FRI,SAT; private WeekDay(){ System.out.println("first");} private WeekDay(int day){ System.out.println("second"); } } public enum TrafficLamp{ RED(30){ @Override public TrafficLamp nextlamp() { // TODO Auto-generated method stub return GREEN; } }, GREEN(45){ @Override public TrafficLamp nextlamp() { // TODO Auto-generated method stub return YELLOW; } }, YELLOW(5){ @Override public TrafficLamp nextlamp() { // TODO Auto-generated method stub return RED; } }; public abstract TrafficLamp nextlamp(); private int time; private TrafficLamp(int time){this.time=time;}; } }