標籤:
原文網址:http://www.itzhai.com/java-based-notes-introduction-and-use-of-an-enumeration-type-static-import.html#1.2、values方法的使用:
Java基礎筆記 – 枚舉類型的使用介紹和靜態匯入本文由arthinking發表於4年前 | Java基礎 | 暫無評論 | 被圍觀 8,332 views+1、枚舉(Enum):1.1、枚舉類型中的兩個靜態方法:1.2、values方法的使用:1.2、values方法的使用:1.3、建立包含私人成員變數的枚舉常量:1.4、枚舉類型詳細說明:1.5、枚舉的比較:1.6、枚舉的相關方法:2、EnumSet:2.1、of方法2.2、noneOf方法3、List儲存枚舉類型:4、EnumMap類:5、枚舉在實際開發中的使用:6、靜態匯入:
1、枚舉(Enum):
JDK5.0中加入了枚舉類型,使用enum關鍵字定義,可以按照如下定義:
public enum Weather{ Sunny, Rainy, Cloudy,}1.1、枚舉類型中的兩個靜態方法:
values() 擷取枚舉類型的所有枚舉常量
valueOf(Class<T> enumType, String name)返回帶指定名稱的指定枚舉類型的枚舉常量。
1.2、values方法的使用:
for(Weather weather : Weather.values()){ System.out.println(weather);}System.out.println(Weather.valueOf(Weather.class, "Sunny"));1.2、values方法的使用:
for(Weather weather : Weather.values()){ System.out.println(weather);}//以下輸出為SunnySystem.out.println(Weather.valueOf(Weather.class, "Sunny"));1.3、建立包含私人成員變數的枚舉常量:
public enum Weather{ Sunny("晴天"), Rainy("雨天"), Cloudy("多雲"); //私人成員變數,儲存名稱 private String value; public String getValue() { return value; } //帶參建構函式 Weather(String value){ this.value = value; }}public static void main(String[] args) { Weather weather1 = Weather.Sunny; //以下輸出為"晴天" System.out.println(weather1.getValue());}1.4、枚舉類型詳細說明:
enum關鍵字的作用類似於class或interface,本質上是在定義一個類別,細節的實現由編譯器完成。
自訂的枚舉類型實質上繼承自java.lang.Enum抽象類別。而每一個成員常量其實就是自己定義的枚舉類型的一個執行個體,都被定義為final,所有無法改變他們,另外他們是static,pulibc的,即:
public static final 枚舉常量;
在運行期間我們無法再使用該枚舉類型建立新的執行個體,這些執行個體是在編譯期間就完全確定下來了的。
1.5、枚舉的比較:
compareTopublic final int compareTo(E o)比較此枚舉與指定對象的順序。在該對象小於、等於或大於指定對象時,分別返回負整數、零或正整數。 枚舉常量只能與相同枚舉類型的其他枚舉常量進行比較。該方法實現的自然順序就是聲明常量的順序。指定者:介面 Comparable<E extends Enum<E>> 中的 compareTo參數:o - 要比較的對象。返回:負整數、零或正整數,根據此對象是小於、等於還是大於指定對象。
Weather[] arrays = Weather.values();for(Weather weather2 : arrays){ System.out.println((Weather.Sunny).compareTo(weather2));}1.6、枚舉的相關方法:
ordinalpublic final int ordinal()返回枚舉常量的序數(它在枚舉聲明中的位置,其中初始常量序數為零)。 大多數程式員不會使用此方法。它被設計用於複雜的基於枚舉的資料結構,比如 EnumSet 和 EnumMap。返回:枚舉常量的序數
for(Weather weather2 : arrays){ System.out.println(weather2.ordinal() + ":" + weather2);}2、EnumSet:
public abstract class EnumSet<E extends Enum<E>>extends AbstractSet<E>implements Cloneable, Serializable
與枚舉類型一起使用的專用 Set 實現。枚舉 set 中所有鍵都必須來自單個枚舉類型,該枚舉類型在建立 set 時顯式或隱式地指定。枚舉 set 在內部表示為位向量。此表示形式非常緊湊且高效。此類的空間和時間效能應該很好,足以用作傳統上基於 int 的“位標誌”的替換形式,具有高品質、型別安全的優勢。如果其參數也是一個枚舉 set,則大量操作(如 containsAll 和 retainAll)也應運行得非常快。
此類可以協助我們建立枚舉值的集合,裡面提供了一系列的靜態方法,可以指定不同的集合建立方式。
2.1、of方法
public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest)建立一個最初包含指定元素的枚舉 set。此Factory 方法的參數列表使用變數參數功能,該方法可以建立一個最初包含任意個元素的枚舉 set,但是這樣很可能比不使用變數參數的重載運行得慢。參數:first - 此 set 最初要包含的元素rest - 此 set 最初要包含的其餘元素返回:最初包含指定元素的枚舉 set拋出:NullPointerException - 如果任意參數為 null,或 rest 為 null
EnumSet<Weather> enumSet = EnumSet.of(Weather.Sunny, Weather.Rainy);for(Iterator<Weather> iter = enumSet.iterator(); iter.hasNext();){ System.out.println(iter.next());}2.2、noneOf方法
public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType)建立一個具有指定元素類型的空枚舉 set。參數:elementType - 此枚舉 set 的元素類型的 class 對象拋出:NullPointerException - 如果 elementType 為 null
EnumSet<Weather> enumSet2 = EnumSet.noneOf(Weather.class);enumSet2.add(Weather.Sunny);enumSet2.add(Weather.Rainy);for(Iterator<Weather> iter = enumSet2.iterator(); iter.hasNext();){ System.out.println(iter.next());}3、List儲存枚舉類型:
List<Weather> list = new ArrayList<Weather>();list.add(Weather.Sunny);list.add(Weather.Cloudy);for(Iterator<Weather> iter = list.iterator(); iter.hasNext();){ System.out.println(iter.next());}4、EnumMap類:
public class EnumMap<K extends Enum<K>,V> extends AbstractMap<K,V> implements Serializable, Cloneable
與枚舉類型鍵一起使用的專用 Map 實現。枚舉映射中所有鍵都必須來自單個枚舉類型,該枚舉類型在建立映射時顯式或隱式地指定。枚舉映射在內部表示為數組。此表示形式非常緊湊且高效。
枚舉映射根據其鍵的自然順序 來維護(該順序是聲明枚舉常量的順序)。在 collection 視圖(keySet()、entrySet() 和 values())所返回的迭代器中反映了這一點。
EnumMappublic EnumMap(Class<K> keyType)建立一個具有指定鍵類型的空枚舉映射。參數:keyType - 此枚舉映射的鍵類型的 class 對象拋出:NullPointerException - 如果 keyType 為空白
Map<Weather, String> enumMap = new EnumMap<Weather, String>(Weather.class);enumMap.put(Weather.Sunny, "晴天");enumMap.put(Weather.Rainy, "雨天");
5、枚舉在實際開發中的使用:
public static String getString(Weather weather){ if(weather == Weather.Sunny){ return Weather.Sunny.getValue(); } else if(weather == Weather.Rainy){ return Weather.Rainy.getValue(); } else if(weather == Weather.Cloudy){ return Weather.Cloudy.getValue(); } return "不符合的天氣情況";}
調用上面的方法:
Weather weather3 = Weather.Sunny;System.out.println(getString(weather3));
6、靜態匯入:
如果要使用靜態變數或者方法,必須給出該方法所在的類。而使用靜態匯入可以使被匯入類的所有靜態變數和靜態方法在當前類直接可見,這樣就無需給出他們的類名了:
如加入以下匯入語句:
import static java.util.EnumSet.noneOf;
就可以直接使用這個靜態方法了:
EnumSet<Weather> enumSet2 = noneOf(Weather.class);enumSet2.add(Weather.Sunny);enumSet2.add(Weather.Rainy);
除了文章中有特別說明,均為IT宅原創文章,轉載請以連結形式註明出處。
本文連結:http://www.itzhai.com/java-based-notes-introduction-and-use-of-an-enumeration-type-static-import.html關鍵字: Enum, Java, 枚舉, 靜態匯入arthinking指彈吉他 && 技術more
【轉】Java基礎筆記 – 枚舉類型的使用介紹和靜態匯入--不錯