package jiaqiang;
public class Enum {
public static void main(String[] args) {
// TODO Auto-generated method stub
//拿到枚举
WeekDay weekday = WeekDay.MON ;
//枚举中的方法
System.out.println(weekday.name());//获取名字
System.out.println(weekday.ordinal());//找到其位置
System.out.println(weekday.valueOf("SUN"));//tostring方法将其变为weekday对象
System.out.println(weekday.values().length);//变换为数组得到数组个数
}
//定义枚举weekday只包含这八个东西
public enum WeekDay{
SUN(1),MON(1),TUE,WED,THT,FRT,FRI,SAT;
//创建空构造函数
private WeekDay(){System.out.println("1");};
//创建有参构造
private WeekDay(int day){System.out.println("2");};
}
//定义交通灯枚举
public enum TrafficLamp{
//子类对象红灯
RED(30){
public TrafficLamp nextLamp(){
return GREEN;
}
},
//子类对象green灯
GREEN(45){
public TrafficLamp nextLamp(){
return YELLOW;
}
},
//子类对象黄灯
YELLOW(5){
public TrafficLamp nextLamp(){
return RED;
}
};
//抽象方法
public abstract TrafficLamp nextLamp();
private int time;
//有参构造
private TrafficLamp(int time){this.time=time;}
//空参构造
private TrafficLamp(){}
}
}
这里创建的枚举中的子类对象为什么不用new出来 |
|