java 中枚举是一个特殊的类,一般表示一组常量。
创建枚举类、使用 enum 关键字定义,常量之间使用逗号分割
public enum Color {
RED,
GREEN,
BLUE
}
使用 javap Color.class 对编译后的源代码进行反编译
E:\>javac Color.java E:\>javap Color.class Compiled from "Color.java" public final class Color extends java.lang.Enum<Color> { public static final Color RED; public static final Color GREEN; public static final Color BLUE; public static Color[] values(); public static Color valueOf(java.lang.String); static {}; }
如上图可以看到:
-
Color 枚举类被 final 修饰,所以枚举类不能被继承
-
Color 枚举类继承了 Enum 这个类,由于java 是单继承的,所以自定义的枚举类不能再继承其他类
-
枚举类中的元素默认的修饰符为 public static final
-
枚举类中元素的类型为该枚举类型(Color类型)
-
编译后编译器会自动生成两个方法 values方法和 valueOf 方法
-
values 方法 : 以数组的形式返回该枚举类中的所有元素
-
valueOf 方法 :返回具有指定名称的指定枚举类型的枚举常量
-
Enum 类
package java.lang;import java.io.Serializable;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;/**
* Enum 实现了 Comparable 和 Serializable 接口,说明枚举类支持排序和序列化
*
*/
public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable {
/**
* 枚举常量的名称
*/
private final String name;/**
* 返回枚举常量的名称,获取枚举名称时优先使用 toString 方法,
*/
public final String name() {
return name;
}/**
* 枚举常量的序数
*/
private final int ordinal;/**
* 返回此枚举常量的序数,从 0 开始
*/
public final int ordinal() {
return ordinal;
}/**
* 受保护的 构造函数,不能使用
*/
protected Enum(String name, int ordinal) {
this.name = name;
this.ordinal = ordinal;
}/**
* 返回枚举常量的名称
*/
public String toString() {
return name;
}/**
* 比较两个枚举对象是否相等
*/
public final boolean equals(Object other) {
return this==other;
}/**
* 返回 hashcode 值
*/
public final int hashCode() {
return super.hashCode();
}/**
* 这个方法保证了枚举不会被克隆是 单实例的
*/
protected final Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}/**
* 将此枚举与指定的对象进行比较以进行排序。* 当此对象小于、等于或大于指定对象时,返回一个负整数、零或正整数
*/
public final int compareTo(E o) {
Enum<?> other = (Enum<?>)o;
Enum<E> self = this;
if (self.getClass() != other.getClass() && // optimization
self.getDeclaringClass() != other.getDeclaringClass())
throw new ClassCastException();
return self.ordinal - other.ordinal;
}/**
* 返回枚举类型对应的 Class 对象
*/
@SuppressWarnings("unchecked")
public final Class<E> getDeclaringClass() {
Class<?> clazz = getClass();
Class<?> zuper = clazz.getSuperclass();
return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
}/**
* 返回具有指定名称的指定enum类型的enum常量。
* 名称必须与用于声明此类型的enum常量的标识符完全匹配。(不允许多余的空格字符。)
*/
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 constant " + enumType.getCanonicalName() + "." + name);
}/**
* enum 不能有 finalize 方法.
*/
protected final void finalize() { }/**
* 防止默认反序列化
*/
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");
}
}
枚举的其他使用方式
/**
* Color 枚举类
* 使用方法: 类名.变量名,和使用静态变量相同
*
* @author xiaoXi
*/
public enum Color {// 枚举变量的定义必须放在第一行,变量之间以 , 分隔
RED("红色"),
BLUE("蓝色"),
// 如果没有其他额外的成员(name),可以不用加 ;
GREEN("绿色");// 其他的成员
private String name;// 构造方法默认并且强制是私有的,可以通过查看字节码文件发现
Color(String name) {
this.name = name;
}public String getName() {
return this.name;
}
}
使用 javap Color.class 对编译后的源代码进行反编译
E:\>javac Color.java E:\>javap Color.class Compiled from "Color.java" public final class Color extends java.lang.Enum<Color> { public static final Color RED; public static final Color BLUE; public static final Color GREEN; public static Color[] values(); public static Color valueOf(java.lang.String); public java.lang.String getName(); static {}; }
public enum Singleton {
SINGLETON
}