枚舉類存在於 java.lang 包中。它是所有Java語言枚舉類型的公共基類。有關枚舉的信息,請參閱enum in java
Class Declaration
public abstract class Enum<E extends Enum> extends Object implements Comparable, Serializable
正如我們所看到的,Enum 是一個Abstract類,所以我們不能創建 Enum 類的對象。
Methods in Enum Class
Enum 類提供了 10 個有用的方法。其中大多數是從 Object 類覆蓋的。這些方法在 Enum 類中聲明為 Final,因此程序員無法修改任何枚舉常量。
- 最終字符串name():此方法返回此枚舉常量的名稱,該名稱與其枚舉聲明中的聲明完全相同。
Syntax : public final String name() Parameters : NA 返回: the name of this enum constant
// Java program to demonstrate name() method enum Color { RED, GREEN, BLUE; } public class Test { // Driver method public static void main(String[] args) { Color c1 = Color.RED; System.out.print("Name of enum constant: "); // name method System.out.println(c1.name()); } }
輸出:
Name of enum constant: RED
- 最終int ordinal():該方法返回該枚舉常量的索引。
Syntax : public final int ordinal() Parameters : NA 返回: the ordinal of this enumeration constant
// Java program to demonstrate ordinal() method enum Color { RED, GREEN, BLUE; } public class Test { // Driver method public static void main(String[] args) { Color c1 = Color.GREEN; System.out.print("ordinal of enum constant "+c1.name()+" : "); // ordinal method System.out.println(c1.ordinal()); } }
輸出:
ordinal of enum constant GREEN : 1
- 字符串toString():此方法返回表示此枚舉常量的 String 對象。此方法與name()方法相同。
Syntax : public String toString() Parameters : NA 返回: a string representation of this enumeration constant Overrides : toString in class Object
// Java program to demonstrate toString() method enum Color { RED, GREEN, BLUE; } public class Test { // Driver method public static void main(String[] args) { Color c1 = Color.GREEN; // getting string representation of enum // using toString() method String str = c1.toString(); System.out.println(str); } }
輸出:
GREEN
- 最終布爾值 equals(Object obj):如果指定的對象等於此枚舉常量,則此方法返回 true,否則返回 false。
Syntax : public final boolean equals(Object obj) Parameters : obj - the object too be compared for equality with this enum. 返回: true if the specified object is equal to this enum constant. false otherwise Overrides : equals in class Object
// Java program to demonstrate equals() method enum Color { RED, GREEN, BLUE; } public class Test { // Driver method public static void main(String[] args) { Color c1 = Color.RED; Color c2 = Color.GREEN; Color c3 = Color.RED; // checking equality between enums // using equals() method boolean b1 = c1.equals(c2); boolean b2 = c1.equals(c3); boolean b3 = c2.equals(null); System.out.println("is c1 equal to c2 : " + b1); System.out.println("is c1 equal to c3 : " + b2); System.out.println("is c2 equal to null : " + b3); } }
輸出:
is c1 equal to c2 : false is c1 equal to c3 : true is c2 equal to null : false
- 最終int hashCode():此方法返回此枚舉常量的哈希碼。實際上這個方法隻包含一條語句,即“return super.hashCode()”,該語句又調用了Object類的hashCode()方法。
Syntax : public final int hashCode() Parameters : NA 返回: a hash code for this enum constant. Overrides : hashCode in class Object
// Java program to demonstrate hashCode() method enum Color { RED, GREEN, BLUE; } public class Test { // Driver method public static void main(String[] args) { Color c1 = Color.RED; System.out.print("hashcode of enum constant "+ c1.name() +" : "); // hashcode method System.out.println(c1.hashCode()); Color c2 = Color.GREEN; System.out.print("hashcode of enum constant "+ c2.name() +" : "); // hashcode method System.out.println(c2.hashCode()); } }
輸出:
hashcode of enum constant RED : 366712642 hashcode of enum constant GREEN : 1829164700
- 最終intcompareTo(E obj):這個方法“compares”這個枚舉與指定對象為次序。枚舉常量隻能與相同枚舉類型的其他枚舉常量進行比較。
Syntax : public int compareTo(E obj) Parameters : obj - the object to be compared. 返回: a negative integer if this object is at less ordinal than the specified object zero if this object is at equal ordinal than the specified object a positive integer if this object is at greater ordinal than the specified object Throws : NullPointerException - if the argument is null
// Java program to demonstrate compareTo() method enum Color { RED, GREEN, BLUE; } public class Test { // Driver method public static void main(String[] args) { Color c1 = Color.RED; Color c2 = Color.GREEN; Color c3 = Color.RED; Color c4 = Color.BLUE; System.out.print("Comparing "+c1.name()+" with "+ c2.name() +" : "); // compareTo method System.out.println(c1.compareTo(c2)); System.out.print("Comparing "+c1.name()+" with "+ c3.name() +" : "); // compareTo method System.out.println(c1.compareTo(c3)); System.out.print("Comparing "+c4.name()+" with "+ c2.name() +" : "); // compareTo method System.out.println(c4.compareTo(c2)); // The following statement throw NullPointerException // as argument of compareTo method is null // System.out.println(c4.compareTo(null)); } }
輸出:
Comparing RED with GREEN : -1 Comparing RED with RED : 0 Comparing BLUE with GREEN : 1
- static <T extends Enum> T valueOf(類枚舉類型,字符串名稱):該方法返回具有指定名稱的指定枚舉類型的枚舉常量。該名稱必須與用於聲明此類型的枚舉常量的標識符完全匹配。
Syntax : public static <T extends Enum> T valueOf(Class enumType,String name) TypeParameters : T - The enum type whose constant is to be returned Parameters : enumType - the Class object of the enum type from which to return a constant name - the name of the constant to return 返回: the enum constant of the specified enum type with the specified name Throws : IllegalArgumentException - if the specified enum type has no constant with the specified name or the specified class object does not represent an enum type NullPointerException - if enumType or name is null
// Java program to demonstrate valueOf() method enum Color { RED, GREEN, BLUE; } public class Test { // Driver method public static void main(String[] args) { // getting value of enum with specified String // using valueOf method Color c1 = Color.valueOf("RED"); Color c2 = Color.valueOf("GREEN"); // name method System.out.println(c1.name()); System.out.println(c2.name()); // The following statement throw IllegalArgumentException // as GrEEN is not an enum constant // Color c3 = Color.valueOf("GrEEN"); // The following statement throw NullPointerException // as argument of valueOf method is null // Color c4 = Color.valueOf(null); } }
輸出:
RED GREEN
- 最終類<E>getDeclaringClass():該方法返回與該枚舉常量的枚舉類型對應的 Class 對象。任意 如果此方法返回相同的 Class 對象,則兩個枚舉常量 e1 和 e2 具有相同的枚舉類型。
Syntax : public final Class <E> getDeclaringClass() Parameters : NA 返回: the Class object corresponding to this enum constant's enum type
// Java program to demonstrate getDeclaringClass() method enum Color { RED, GREEN, BLUE; } enum Day { MONDAY, TUESDAY ; } public class Test { // Driver method public static void main(String[] args) { // getting value of enum with specified String // using valueOf method Color c1 = Color.valueOf("RED"); Color c2 = Color.valueOf("GREEN"); Day d1 = Day.valueOf("MONDAY"); Day d2 = Day.valueOf("TUESDAY"); System.out.print("Class corresponding to "+ c1.name() +" : "); // getDeclaringClass method System.out.println(c1.getDeclaringClass()); System.out.print("Class corresponding to "+ c2.name() +" : "); // getDeclaringClass method System.out.println(c2.getDeclaringClass()); System.out.print("Class corresponding to "+ d1.name() +" : "); // getDeclaringClass method System.out.println(d1.getDeclaringClass()); System.out.print("Class corresponding to "+ d2.name() +" : "); // getDeclaringClass method System.out.println(d2.getDeclaringClass()); } }
輸出:
Class corresponding to RED : class Color Class corresponding to GREEN : class Color Class corresponding to MONDAY : class Day Class corresponding to TUESDAY : class Day
- 最終對象clone():此方法保證枚舉永遠不會被克隆,這是保留其 “singleton” 狀態所必需的。編譯器在內部使用它來創建 Enum 常量。
Syntax : public final Object clone() throws CloneNotSupportedException Parameters : NA 返回: NA Overrides : clone in class Object Throws : CloneNotSupportedException-if the object's class does not support the Cloneable interface.
// Java program to demonstrate clone() method enum Color { RED, GREEN, BLUE; } public class Test { // Driver method public static void main(String[] args) throws CloneNotSupportedException { System.out.println("Enums are never cloned"); Test t = new Test() { // final clone method protected final Object clone() throws CloneNotSupportedException { return new CloneNotSupportedException(); } }; System.out.println(t.clone()); } }
輸出:
Enums are never cloned java.lang.CloneNotSupportedException
- 最終無效finalize():該方法保證枚舉類不能有finalize方法。
Syntax : protected final void finalize() Parameters : NA 返回: NA Overrides : finalize in class Object
// Java program to demonstrate finalize() method enum Color { RED, GREEN, BLUE; } public class Test { // Driver method public static void main(String[] args) throws Throwable { System.out.println("enum classes cannot have finalize methods"); Test t = new Test() { // final finalize method protected final void finalize() throws Throwable { // empty implementation }; }; } }
輸出:
enum classes cannot have finalize methods
相關用法
- Java Java.lang.Enum.clone()用法及代碼示例
- Java Java.lang.Enum.compareTo()用法及代碼示例
- Java Java.lang.Enum.equals()用法及代碼示例
- Java Java.lang.Enum.finalize()用法及代碼示例
- Java Java.lang.Enum.getDeclaringClass()用法及代碼示例
- Java Java.lang.Enum.hashCode()用法及代碼示例
- Java Java.lang.Enum.name()用法及代碼示例
- Java Java.lang.Enum.ordinal()用法及代碼示例
- Java Java.lang.Enum.toString()用法及代碼示例
- Java Java.lang.Enum.valueOf()用法及代碼示例
- Java Java.lang.Boolean.booleanValue()用法及代碼示例
- Java Java.lang.Boolean.compareTo()用法及代碼示例
- Java Java.lang.Boolean.equals()用法及代碼示例
- Java Java.lang.Boolean.getBoolean()用法及代碼示例
- Java Java.lang.Boolean.hashCode()用法及代碼示例
- Java Java.lang.Boolean.parseBoolean()用法及代碼示例
- Java Java.lang.Boolean.toString()用法及代碼示例
- Java Java.lang.Boolean.valueOf()用法及代碼示例
- Java Java.lang.Byte.byteValue()用法及代碼示例
- Java Java.lang.Byte.compareTo()用法及代碼示例
- Java Java.lang.Byte.decode()用法及代碼示例
- Java Java.lang.Byte.doubleValue()用法及代碼示例
- Java Java.lang.Byte.equals()用法及代碼示例
- Java Java.lang.Byte.floatValue()用法及代碼示例
- Java Java.lang.Byte.hashCode()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.lang.Enum Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。