public class test {
public static void main(String[] args) {
WeekDay w = WeekDay.MON;
System.out.println(w);//自己会调用tostring方法
System.out.println(w.ordinal());//打印是枚举列表第几个对象
System.out.println(WeekDay.values().length);//一共有多少个枚举对象
}
public enum WeekDay{
SAT,MON,TUE,WED,THU,FRI,SAT,
private WeekDay(){
System.out.println("11");
}
private WeekDay(int a){
System.out.println("a");
}
}
}
首先上面定义一个简单的枚举类WeekDay
该类中的sat,mon..等实际上就是weekday类的对象
注意:
枚举类也有构造方法,构造方法必须是私有的;
通过下面代码应该就可以了解枚举类型的使用了,要结合内部类来理解
public enum TrefficLamp{
RED(30){//红灯对象调用参数为int型的构造方法,
public TrefficLamp nextLamp(){
return GREEN;
}
},
GREEN(20){
public TrefficLamp nextLamp(){
return YELLOW;
}
},
YELLOW(2){
public TrefficLamp nextLamp(){
return RED;
}
};
public abstract TrefficLamp nextLamp();
private int time;
private TrefficLamp(int time){
this.time = time;
};
}