抽象的な方法を伴う列挙を実現する
列挙のさらなる深化は、抽象的な方法を伴う列挙を実現し、交通灯を例に、コードは以下の通りである.
出力の結果は次のとおりです.
-------------------------------------------------------------------------------------------------
GREEN this time is 30 -------------------------------------------------------------------------------------------------
上の列挙TrafficLampでは赤緑黄の3種類の交通信号を実現し、抽象的な方法でNextLampの次のランプを実現しました!個人的には強い感じがします!
package com.jemsn.weekday;
public class test {
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;
}
}
}
出力の結果は次のとおりです.
-------------------------------------------------------------------------------------------------
GREEN this time is 30 -------------------------------------------------------------------------------------------------
上の列挙TrafficLampでは赤緑黄の3種類の交通信号を実現し、抽象的な方法でNextLampの次のランプを実現しました!個人的には強い感じがします!