- public class EnumDemo {
- public static void main(String[] args) {
- TrafficLamp red=TrafficLamp.Red;
- System.out.println(red.NextLamp());
- System.out.println("this time is "+red.Time);
- }
- public enum TrafficLamp{
- Red(30){
- public TrafficLamp NextLamp() {
- return 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;
- }
- }
- }
复制代码 |