一.枚举类学习:
public class EnumTest{
public static void main(String[] args){
WeekDay weekday = WeekDay.FRI;
System.out.println(weekDay);
System.out.println(weekDay.valueof("SUN").toString());
System.out.println(weekDay.values().length);
}
public enum WeekDay{
SUN,MON,TUE,WED,THI,FRI,SAT
}
}
二.实现带有构造方法的枚举
public enum WeekDay{
SUN,MON,TUE,WED,THI,FRI,SAT
private WeekDay(){System.out.println("first");}//必须为私有,必须在枚举对象后面,枚举对象后面有分号
private WeekDay(int day){System.out.println("second")}//多个构造方法时,可以通过这样的方式指明调用哪个构造方法:SUN(1),MON();
}
三.实现带有抽象方法的枚举
public enum TrafficLamp(){
RED(30){
public TrafficLamp nextLamp(){//实现抽象方法
return YELLOW;
}
}
}
public abstract TrafficLamp nextLamp();//抽象方法
private int time;//私有成员
private TrafficLamp(int time){this.time = time;}
}
枚举类只有一个成员时,就可以作为一种单例的实现方式 |
|