JDK源码解析 java.lang.Enum类
?
package java.lang;
?
import java.io.Serializable;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
?
/**
? ? 这是所有 Java 语言枚举类型的公共基本类。?
?
? ? 从以下版本开始:?
? ? 1.5?
? ? 另请参见:
? ? 序列化表格
?*/
//枚举类型其实就是一个抽象类,并且实现了Comparable、Serializable两个接口
public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable {?
? ? /**
? ? ? 枚举常量的名称
? ? ?*/
? ? private final String name;
?
? ? /**
? ? ? 返回此枚举常量的名称,在其枚举声明中对其进行声明。 与此方法相比,大多数程序员应该优先考虑使用 toString() 方法,因为 toString 方法返回更加用户友好的名称。该方法主要设计用于特殊情形,其正确性取决于获取正确的名称,其名称不会随版本的改变而改变。?
?
? ? ? 返回:
? ? ? 枚举常量的名称
? ? ?*/
? ? public final String name() {
? ? ? ? return name;
? ? }
?
? ? /**
? ? ? 枚举常量的序数
? ? ?*/
? ? private final int ordinal;
?
? ? /**
? ? ? ? 返回枚举常量的序数(它在枚举声明中的位置,其中初始常量序数为零)。 大多数程序员不会使用此方法。它被设计用于复杂的基于枚举的数据结构,比如 EnumSet 和 EnumMap。?
?
? ? ? ? 返回:
? ? ? ? 枚举常量的序数
? ? ?*/
? ? public final int ordinal() {
? ? ? ? return ordinal;
? ? }
?
? ? /**
? ? ? ? 单独的构造方法。程序员无法调用此构造方法。该构造方法用于由响应枚举类型声明的编译器发出的代码。?
?
? ? ? ? 参数:
? ? ? ? name - - 此枚举常量的名称,它是用来声明该常量的标识符。
? ? ? ? ordinal - - 枚举常量的序数(它在枚举声明中的位置,其中初始常量序数为零)。
? ? ?*/
? ? protected Enum(String name, int ordinal) {
? ? ? ? this.name = name;
? ? ? ? this.ordinal = ordinal;
? ? }
?
? ? /**
? ? ? 返回枚举常量的名称,它包含在声明中。可以重写此方法,虽然一般来说没有必要。当存在更加“程序员友好的”字符串形式时,应该使用枚举类型重写此方法。?
?
? ? ? 覆盖:
? ? ? 类 Object 中的 toString
? ? ? 返回:
? ? ? 枚举常量的名称
? ? ?*/
? ? public String toString() {
? ? ? ? return name;
? ? }
?
? ? /**
? ? ? 当指定对象等于此枚举常量时,返回 true。?
?
? ? ? 覆盖:
? ? ? 类 Object 中的 equals
? ? ? 参数:
? ? ? other - 要与此对象进行相等性比较的对象。?
? ? ? 返回:
? ? ? 如果指定对象等于此枚举常量,则返回 true。
? ? ? 另请参见:
? ? ? Object.hashCode(), Hashtable
? ? ?*/
? ? public final boolean equals(Object other) {
? ? ? ? return this == other;
? ? }
?
? ? /**
? ? ? 返回枚举常量的哈希码。?
?
? ? ? 覆盖:
? ? ? 类 Object 中的 hashCode
? ? ? 返回:
? ? ? 枚举常量的哈希码。
? ? ? 另请参见:
? ? ? Object.equals(java.lang.Object), Hashtable
? ? ?*/
? ? public final int hashCode() {
? ? ? ? return super.hashCode();
? ? }
?
? ? /**
? ? ? 抛出 CloneNotSupportedException。这可保证永远不会复制枚举,这对于保留其“单元素”状态是必需的。?
?
? ? ? 覆盖:
? ? ? 类 Object 中的 clone
? ? ? 返回:
? ? ? 此实例的一个副本。?
? ? ? 抛出:?
? ? ? CloneNotSupportedException - 如果对象的类不支持 Cloneable 接口,则重写 clone 方法的子类也会抛出此异常,以指示无法复制某个实例。
? ? ? 另请参见:
? ? ? Cloneable
? ? ?*/
? ? protected final Object clone() throws CloneNotSupportedException {
? ? ? ? throw new CloneNotSupportedException();
? ? }
?
? ? /**
? ? ? 比较此枚举与指定对象的顺序。在该对象小于、等于或大于指定对象时,分别返回负整数、零或正整数。 枚举常量只能与相同枚举类型的其他枚举常量进行比较。该方法实现的自然顺序就是声明常量的顺序。?
?
? ? ? 指定者:
? ? ? 接口 Comparable<E extends Enum<E>> 中的 compareTo
? ? ? 参数:
? ? ? o - 要比较的对象。?
? ? ? 返回:
? ? ? 负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。
? ? ?*/
? ? public final int compareTo(E o) {
? ? ? ? Enum other = (Enum) o;
? ? ? ? Enum self = this;
? ? ? ? if (self.getClass() != other.getClass() && // optimization
? ? ? ? ? ? ? ? self.getDeclaringClass() != other.getDeclaringClass())
? ? ? ? ? ? throw new ClassCastException();
? ? ? ? return self.ordinal - other.ordinal;
? ? }
?
? ? /**
? ? ? 返回与此枚举常量的枚举类型相对应的 Class 对象。当且仅当 e1.getDeclaringClass() == e2.getDeclaringClass() 时,两个枚举常量 e1 和 e2 的枚举类型才相同。(由该方法返回的值不同于由 Object.getClass() 方法返回的值,Object.getClass() 方法用于带有特定常量的类主体的枚举常量。)?
?
? ? ? 返回:
? ? ? 与此枚举常量的枚举类型相对应的 Class 对象
? ? ?*/
? ? public final Class<E> getDeclaringClass() {
? ? ? ? Class clazz = getClass();
? ? ? ? Class zuper = clazz.getSuperclass();
? ? ? ? return (zuper == Enum.class) ? clazz : zuper;
? ? }
?
? ? /**
? ? ? 返回带指定名称的指定枚举类型的枚举常量。名称必须与在此类型中声明枚举常量所用的标识符完全匹配。(不允许使用额外的空白字符。)?
?
? ? ? 参数:
? ? ? enumType - 要从中返回常量的枚举类型的 Class 对象
? ? ? name - 要返回的常量名称?
? ? ? 返回:
? ? ? 带指定名称的指定枚举类型的枚举常量?
? ? ? 抛出:?
? ? ? IllegalArgumentException - 如果指定枚举类型不包含指定名称的常量,或者指定类对象不表示枚举类型?
? ? ? NullPointerException - 如果 enumType 或 name 为空
? ? ? 从以下版本开始:?
? ? ? 1.5?
? ? ?*/
? ? public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) {
? ? ? ? T result = enumType.enumConstantDirectory().get(name);
? ? ? ? if (result != null)
? ? ? ? ? ? return result;
? ? ? ? if (name == null)
? ? ? ? ? ? throw new NullPointerException("Name is null");
? ? ? ? throw new IllegalArgumentException("No enum const " + enumType + "." + name);
? ? }
?
? ? /**
? ? ?* prevent default deserialization
? ? ?*/
? ? private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
? ? ? ? throw new InvalidObjectException("can't deserialize enum");
? ? }
?
? ? private void readObjectNoData() throws ObjectStreamException {
? ? ? ? throw new InvalidObjectException("can't deserialize enum");
? ? }
?
? ? /**
? ? ? ? 枚举类不能有 finalize 方法。?
?
? ? ? ? 覆盖:
? ? ? ? 类 Object 中的 finalize
? ? ?*/
? ? protected final void finalize() {
? ? }
}