Java 之 enum

??? 关键字enum可以将一组具名的值的有限集合创建为一种新的类型,而这些具名的值可以作为常规的程序的组件使用。这是一种非常有用的功能。创建一个enum时,编译器会生成一个相关的类该类继承自?java.lang.Enum.通过Enum?这个类的源码可以看到有这样几个方法?name(),ordinal()等方法

???

public abstract class Enum<E extends Enum<E>>
        implements Comparable<E>, Serializable {
  
    private final String name;

   
    public final String name() {
        return name;
    }

   
    private final int ordinal;

   
    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;
    }

   
    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;
    }

    
    @SuppressWarnings("unchecked")
    public final Class<E> getDeclaringClass() {
        Class<?> clazz = getClass();
        Class<?> zuper = clazz.getSuperclass();
        return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
    }

    
    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);
    }

   
    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");
    }
}

?运行的结果

/**
 *
 * @author zhangwei_david
 * @version $Id: EnumDemo.java, v 0.1 2014年10月19日 上午8:37:48 zhangwei_david Exp $
 */
public class EnumDemo {
    enum Const {
        A, B, C, D, E, F, G;
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args) {

        /**测试 values****/
        System.out.println("enum\t ordinal\t compareTo(E)\t equals\t\tx ==E");
        for (Const c : Const.values()) {
            System.out.println(c.name() + "\t " + c.ordinal() + "\t\t" + c.compareTo(Const.E)
                               + "\t\t" + c.equals(Const.E) + "\t\t" + (Const.E == c));
        }

    }
}

?

enum	 ordinal	 compareTo(E)	 equals		x ==E
A	 0		-4		false		false
B	 1		-3		false		false
C	 2		-2		false		false
D	 3		-1		false		false
E	 4		0		true		true
F	 5		1		false		false
G	 6		2		false		false

?

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。