全部的枚舉都繼承自java.lang.Enum類。因爲Java 不支持多繼承,因此枚舉對象不能再繼承其餘類(能夠實現接口)。 html
在JDK1.5以前,咱們定義常量都是:public static fianl....。如今好了,有了枚舉,能夠把相關的常量分組到一個枚舉類型裏,並且枚舉提供了比常量更多的方法。java
package com; public enum Color { RED, GREEN, BLANK, YELLOW }
使用程序員
package com; public class B { public static void main(String[] args) { System.out.println( isRed( Color.BLANK ) ) ; //結果: false System.out.println( isRed( Color.RED ) ) ; //結果: true } static boolean isRed( Color color ){ if ( Color.RED.equals( color )) { return true ; } return false ; } }
或者 switch 的使用編程
package com; public class B { public static void main(String[] args) { showColor( Color.RED ); } static void showColor(Color color){ switch ( color ) { case BLANK: System.out.println( color ); break; case RED : System.out.println( color ); break; default: System.out.println( color ); break; } } }
package com; public enum Color { RED("紅色", 1), GREEN("綠色", 2), BLANK("白色", 3), YELLO("黃色", 4); private String name ; private int index ; private Color( String name , int index ){ this.name = name ; this.index = index ; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } }
使用安全
package com; public class B { public static void main(String[] args) { //輸出某一枚舉的值 System.out.println( Color.RED.getName() ); System.out.println( Color.RED.getIndex() ); //遍歷全部的枚舉 for( Color color : Color.values()){ System.out.println( color + " name: " + color.getName() + " index: " + color.getIndex() ); } } }
結果數據結構
紅色 1 RED name: 紅色 index: 1 GREEN name: 綠色 index: 2 BLANK name: 白色 index: 3 YELLO name: 黃色 index: 4
全部的枚舉都繼承自java.lang.Enum類。因爲Java 不支持多繼承,因此枚舉對象不能再繼承其餘類。 ide
例如:函數
package enumTest; public interface DayInterface { public String getDay(); }
package enumTest; public enum MyDay implements DayInterface{ MONDAY(1,"星期一"),THUSDAY(2,"星期二");//這個後面必須有分號 private int code; private String name; private MyDay(int code,String name) { this.code = code; this.name = name; } public int getCode() { return code; } public String getName() { return name; } public void setCode(int code) { this.code = code; } public void setName(String name) { this.name = name; } @Override public String toString() { // TODO Auto-generated method stub return this.getName()+"---"+this.getCode(); } @Override public String getDay() { return this.getName(); } }
測試:學習
package enumTest; public class Test { public static void main(String[] args) { System.out.println(MyDay.THUSDAY.getDay()); } }
結果:測試
星期二
總結:
一、枚舉的本質是類,在沒有枚舉以前,仍然能夠按照java最基本的編程手段來解決須要用到枚舉的地方。枚舉屏蔽了枚舉值的類型信息,不像在用public static final定義變量必須指定類型。枚舉是用來構建常量數據結構的模板,這個模板可擴展。枚舉的使用加強了程序的健壯性,好比在引用一個不存在的枚舉值的時候,編譯器會報錯。枚舉的更多用法還須要在開發中去研究創造,Java五、Java6增長了很多新的特性,技術在升級,對程序員來講就要學習,若是你熱愛java的話。不然別人用到新特性的代碼你看不懂,那才叫鬱悶。
二、枚舉在Java家族中只佔了很小的一塊比重,因此我在項目中用枚舉的地方不是不少,畢竟,一個項目是不少人開發維護的,用一個陌生的東西,會給其餘的同事形成閱讀困難。因此常量大都是用public static final 來定義的。
參考:
https://www.cnblogs.com/zhaoyanjun/p/5659811.html
https://www.cnblogs.com/qlqwjy/p/9065264.html