public abstract class Enum 
 extends Object implements Comparable<E extends Enum<E>>, Serializable
| java.lang.Object | |
| ↳ | java.lang.Enum<E extends java.lang.Enum<E>> | 
|   |  
      
这是所有Java语言枚举类型的通用基类。 有关枚举的更多信息,包括由编译器合成的隐式声明方法的描述,可以在The Java™ Language Specification的第8.9节中找到 。
请注意,使用枚举类型作为集合的类型或作为地图中键的类型时,可以使用专用且高效的 set和 map实现。
也可以看看:
Protected constructors |  
      |
|---|---|
  Enum(String name, int ordinal)  唯一的构造函数。  |  
      |
Public methods |  
      |
|---|---|
 final int  |  
         compareTo(E o)  将此枚举与指定的对象进行比较以进行排序。  |  
      
 final boolean  |  
         equals(Object other)  如果指定的对象等于此枚举常量,则返回true。  |  
      
 final Class<E>  |  
         getDeclaringClass()  返回与此枚举常量的枚举类型对应的Class对象。  |  
      
 final int  |  
         hashCode()  返回此枚举常量的哈希码。  |  
      
 final String  |  
         name()  返回此枚举常量的名称,与其枚举声明中声明的完全相同。  |  
      
 final int  |  
         ordinal()  返回此枚举常量的序号(它在枚举声明中的位置,其中初始常量赋予一个零次序)。  |  
      
 String  |  
         toString()  返回声明中包含的此枚举常量的名称。  |  
      
 static <T extends Enum<T>> T  |  
         valueOf(Class<T> enumType, String name)  返回具有指定名称的指定枚举类型的枚举常量。  |  
      
Protected methods |  
      |
|---|---|
 final Object  |  
         clone()  引发CloneNotSupportedException。  |  
      
 final void  |  
         finalize()  枚举类不能有finalize方法。  |  
      
Inherited methods |  
      |
|---|---|
  java.lang.Object  
         |  
      |
  java.lang.Comparable  
         |  
      |
Enum (String name, int ordinal)
唯一的构造函数。 程序员不能调用这个构造函数。 它由编译器发出的代码用于响应枚举类型声明。
| Parameters | |
|---|---|
name |  
         String: - The name of this enum constant, which is the identifier used to declare it. |  
       
ordinal |  
         int: - The ordinal of this enumeration constant (its position in the enum declaration, where the initial constant is assigned an ordinal of zero).  |  
       
int compareTo (E o)
将此枚举与指定的对象进行比较以进行排序。 返回负整数,零或正整数,因为此对象小于,等于或大于指定的对象。 枚举常量只能与其他枚举类型的枚举常量相比较。 这个方法实现的自然顺序是声明常量的顺序。
| Parameters | |
|---|---|
o |  
         E 
          |  
       
| Returns | |
|---|---|
int |  
        |
boolean equals (Object other)
如果指定的对象等于此枚举常量,则返回true。
| Parameters | |
|---|---|
other |  
         Object: the object to be compared for equality with this object. |  
       
| Returns | |
|---|---|
boolean |  
        true if the specified object is equal to this enum constant. | 
Class<E> getDeclaringClass ()
返回与此枚举常量的枚举类型对应的Class对象。 两个枚举常量e1和e2具有相同的枚举类型当且仅当e1.getDeclaringClass()== e2.getDeclaringClass()。 (此方法返回的值可能与getClass()方法返回的具有常量特定类体的枚举常量返回的值不同。)
| Returns | |
|---|---|
Class<E> |  
        the Class object corresponding to this enum constant's enum type | 
int hashCode ()
返回此枚举常量的哈希码。
| Returns | |
|---|---|
int |  
        a hash code for this enum constant. | 
String name ()
返回此枚举常量的名称,与其枚举声明中声明的完全相同。 大多数程序员应该优先使用toString()方法,因为toString方法可能会返回一个更加用户友好的名称。 此方法主要用于专门的情况,其中正确性取决于获取确切的名称,从发布到发布不会有所不同。
| Returns | |
|---|---|
String |  
        the name of this enum constant | 
int ordinal ()
返回此枚举常量的序号(它在枚举声明中的位置,其中初始常量赋予一个零次序)。 大多数程序员将无法使用这种方法。 它专为基于枚举的复杂数据结构(如EnumSet和EnumMap而EnumMap 。
| Returns | |
|---|---|
int |  
        the ordinal of this enumeration constant | 
String toString ()
返回声明中包含的此枚举常量的名称。 这种方法可能会被覆盖,尽管它通常不是必需或不可取的。 当一个更“程序员友好”的字符串形式存在时,枚举类型应该重写此方法。
| Returns | |
|---|---|
String |  
        the name of this enum constant | 
T valueOf (Class<T> enumType, String name)
返回具有指定名称的指定枚举类型的枚举常量。 该名称必须完全匹配用于在此类型中声明枚举常量的标识符。 (不允许使用外来的空白字符。)
请注意,对于特定的枚举类型T ,可以使用该枚举中隐式声明的public static T valueOf(String)方法而不是此方法将名称映射到相应的枚举常量。 通过调用该类型的隐式public static T[] values()方法可以获得枚举类型的所有常量。
| Parameters | |
|---|---|
enumType |  
         Class: the Class object of the enum type from which to return a constant |  
       
name |  
         String: the name of the constant to return |  
       
| Returns | |
|---|---|
T |  
        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 |  
       
Object clone ()
引发CloneNotSupportedException。 这保证了枚举永远不会被克隆,这是保持其“单身”状态所必需的。
| Returns | |
|---|---|
Object |  
        (never returns) | 
| Throws | |
|---|---|
CloneNotSupportedException |  
        |