Enum列舉類的學習筆記
小案例:
你寫了一個小程式,不過好久不用了,突然有一天,你想使用一下它。程式要想正確執行,需要將今天星期幾存到資料庫裡。這個時候,你開始犯難了。
當初的你還很年輕,不懂程式界的險惡,設計這個程式的時候,傻不拉幾把這個欄位設計為int型別的,用0代表週日,1代表週一。。。6代表週六,新增的時候就setWeekday(0)。但是這麼長時間沒用了,你忘記自己是從週一開始計算還是週日開始計算了,換句話說,你想不起來0代表的是週一還是週日了!
於是你各種翻程式碼,看資料庫的欄位,資料庫儲存的資訊,終於搞懂了,你很開心,用了一次之後,覺得這個程式沒意思,又不用了。
很久之後,你心血來潮,又想用一次它,很不幸,你又忘記到底0代表週一還是週日了,一番查詢之後。你決定重構程式碼,因為你受不了了!!
-
靜態變數來幫忙
經過一番思考,你決定使用七個靜態變數來代表星期幾,以後只要引用和靜態變數就可以了,而不用自己輸入012….你這麼寫:
1 public class Weekday { 2 public final static int SUN = 0; 3 public final static int MON = 1; 4 public finalstatic int TUE = 2; 5 public final static int WED = 3; 6 public final static int THU = 4; 7 public final static int FRI = 5; 8 public final static int SAT = 6; 9 10 }
機智如你,這個時候,只要 Weekday.SUN 就可以了,不用操心到底應該填寫0還是填寫1。
但是這個時候的你,也不是當初初出茅廬的小夥子了,很明顯,這樣寫已經不能滿足你了。你還想讓這個類做更多的事,
比如,你想知道下一天是星期幾,還想把今天是星期幾打印出來。一番深思熟慮後,你改成了這樣:
1 public class Weekday { 2 3 private Weekday(){} 4 5 public final static Weekday SUN = new Weekday(); 6 public final static Weekday MON = new Weekday(); 7 public final static Weekday TUE = new Weekday(); 8 public final static Weekday WED = new Weekday(); 9 public final static Weekday THU = new Weekday(); 10 public final static Weekday FRI = new Weekday(); 11 public final static Weekday SAT = new Weekday(); 12 13 public static Weekday getNextDay(Weekday nowDay){ 14 if(nowDay == SUN) { 15 return MON; 16 }else if(nowDay == MON) { 17 return TUE; 18 }else if(nowDay == TUE) { 19 return WED; 20 }else if(nowDay == WED) { 21 return THU; 22 }else if(nowDay == THU) { 23 return FRI; 24 }else if(nowDay == FRI) { 25 return SAT; 26 }else { 27 return SUN; 28 } 29 } 30 31 public static void printNowDay(Weekday nowDay){ 32 if(nowDay == SUN) 33 System.out.println("sunday"); 34 else if(nowDay == MON) 35 System.out.println("monday"); 36 else if(nowDay == TUE) 37 System.out.println("tuesday"); 38 else if(nowDay == WED) 39 System.out.println("wednesday"); 40 else if(nowDay == THU) 41 System.out.println("thursday"); 42 else if(nowDay == FRI) 43 System.out.println("friday"); 44 else 45 System.out.println("saturday"); 46 } 47 48 } 49 50 class Test{ 51 public static void main(String[] args) { 52 Weekday nowday = Weekday.SUN; 53 Weekday.printNowDay(nowday); 54 Weekday nextDay = Weekday.getNextDay(nowday); 55 System.out.print("nextday ====> "); 56 Weekday.printNowDay(nextDay); 57 } 58 } 59 //測試結果: 60 //sunday 61 //nextday ====> monday
喲,不錯。考慮的很詳細。並且私有構造方法後,外界就不能建立該類的物件了,這樣就避免了星期八星期九的出現,所有Weekday的物件都在該類內部建立。
不對,好像缺了點什麼,我要的是int!我的int呢?!。所以,你還需要一個這樣的方法:
1 public static int toInt(Weekday nowDay){ 2 if(nowDay == SUN) 3 return 0; 4 else if(nowDay == MON) 5 return 1; 6 else if(nowDay == TUE) 7 return 2; 8 else if(nowDay == WED) 9 return 3; 10 else if(nowDay == THU) 11 return 4; 12 else if(nowDay == FRI) 13 return 5; 14 else 15 return 6; 16 }
- 簡單的列舉類
1 public enum Weekday { 2 SUN,MON,TUS,WED,THU,FRI,SAT 3 }
程式碼這麼少?
沒錯,這就是列舉類,我們來看看怎麼使用它:
1 class Test2{ 2 public static void main(String[] args) { 3 Weekday sun = Weekday.SUN; 4 System.out.println(sun); // 輸出 SUN 5 } 6 }
看起來和上面的靜態變數使用方式差不多,而且預設的toString方法返回的就是對應的名字。
我們上面的那段程式碼重寫toString也是不可以打印出當前是星期幾的,因為toString方法沒有引數。所以我們自己寫了一個printNowDay方法。
當然,這麼簡單的列舉類是不可能實現我們的要求的,所以,我們還要接著寫:
1 public enum Weekday { 2 SUN(0),MON(1),TUS(2),WED(3),THU(4),FRI(5),SAT(6); 3 4 private int value; 5 6 private Weekday(int value){ 7 this.value = value; 8 } 9 10 public static Weekday getNextDay(Weekday nowDay){ 11 int nextDayValue = nowDay.value; 12 13 if (++nextDayValue == 7){ 14 nextDayValue =0; 15 } 16 17 return getWeekdayByValue(nextDayValue); 18 } 19 20 public static Weekday getWeekdayByValue(int value) { 21 for (Weekday c : Weekday.values()) { 22 if (c.value == value) { 23 return c; 24 } 25 } 26 return null; 27 } 28 } 29 30 class Test2{ 31 public static void main(String[] args) { 32 System.out.println("nowday ====> " + Weekday.SAT); 33 System.out.println("nowday int ====> " + Weekday.SAT.ordinal()); 34 System.out.println("nextday ====> " + Weekday.getNextDay(Weekday.SAT)); // 輸出 SUN 35 36 //輸出: 37 //nowday ====> SAT 38 //nowday int ====> 6 39 //nextday ====> SUN 40 } 41 }