public class EnumTest1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(TrafficLight tl:TrafficLight.values()){
System.out.println(tl.toString()+"..."+tl.nextLight());
}
}
}
enum TrafficLight{
RED(30) {
@Override
public TrafficLight nextLight() {
// TODO Auto-generated method stub
return GREEN;
}
},GREEN(40) {
@Override
public TrafficLight nextLight() {
// TODO Auto-generated method stub
return YELLOW;
}
},YELLOW(5) {
@Override
public TrafficLight nextLight() {
// TODO Auto-generated method stub
return RED;
}
};
abstract public TrafficLight nextLight();
private int time;
private TrafficLight(int time){
this.time=time;
}
}
枚举在定义的时候使用关键字enum,感觉和类很相似啊!这其中是不是有什么联系?明白的同学给讲解下吧 |