java 枚举 类 enum

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;  
  
    // 当前枚举常量次序,从0开始  
    public final int ordinal() {  
    return ordinal;  
    }  
  
    // 专有构造器,我们无法调用。该构造方法用于由响应枚举类型声明的编译器发出的代码。   
    protected Enum(String name, int ordinal) {  
    this.name = name;  
    this.ordinal = ordinal;  
    }  
  
    // 返回枚举常量的名称,默认是返回name值。可以重写该方法,输出更加友好的描述。  
    public String toString() {  
    return name;  
    }  
  
    // 比较当前枚举常量是否和指定的对象相等。因为枚举常量是单例的,所以直接调用==操作符。子类不可以重写该方法。  
    public final boolean equals(Object other) {   
        return this==other;  
    }  
  
    // 返回该枚举常量的哈希码。和equals一致,该方法不可以被重写。  
    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 self = this;  
    if (self.getClass() != other.getClass() && // optimization  
            self.getDeclaringClass() != other.getDeclaringClass())  
        throw new ClassCastException();  
    return self.ordinal - other.ordinal;  
    }  
  
    // 得到枚举常量所属枚举类型的Class对象  
    public final Class<E> getDeclaringClass() {  
    Class clazz = getClass();  
    Class zuper = clazz.getSuperclass();  
    return (zuper == Enum.class) ? clazz : 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 const " + enumType +"." + name);  
    }  
  
    // 不允许反序列化枚举对象  
    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方法,子类不可以重写该方法  
    protected final void finalize() { }  
}  
package testenum;


public class TestEnum {
    public enum stats{
        open,off,r,g,b,黄,ff0000;
        
    }
    public enum color{
        
    }
    
    public enum Yes {  
        A(0), B(1), C(2), D(3), F(4);  
      
        private Integer code;  
      
        Yes() {  
        }  
      
        Yes(int code) {  
            this.code = code;  
        }  
      
        public Integer getCode() {  
            return code;  
        }  
    }  
    
    public enum Week {
        MON{
            public String toLocaleString(){
                return "星期一";
            }
            public String toLocaleString2(){
                return "星期一";
            }
        },TUES{
            public String toLocaleString(){
                return "星期二";
            }
        },WEB{
            public String toLocaleString(){
                return "星期三";
            }

        },THUR{
            public String toLocaleString(){
                return "星期四";
            }

        },FRI{
            public String toLocaleString(){
                return "星期五";
            }

        },SAT{
            public String toLocaleString(){
                return "星期六";
            }

        },SUN{
            public String toLocaleString(){
                return "星期日";
            }

        };
        public abstract String toLocaleString();
    }
    public static void main(String [] args){
        System.out.println("《------------------------------------------------------------------》");
        System.out.println(stats.class);
        System.out.println(stats.off);
        System.out.println(stats.open);
        for(int i=0;i<stats.values().length;i++){
            System.out.println("stats.values()["+i+"]:"+stats.values()[i]);
            System.out.println(stats.valueOf(stats.values()[i].toString()));
        }
        

        System.out.println("《------------------------------------------------------------------》");
        System.out.println(Yes.class);
        System.out.println(Yes.A);
        System.out.println(Yes.B.getCode());
        
        for(int i=0;i<Yes.values().length;i++){
            System.out.println("Yes.values()["+i+"]:"+Yes.values()[i]);
            System.out.println(Yes.valueOf(Yes.values()[i].toString()));
        }

        System.out.println("《------------------------------------------------------------------》");
        System.out.println(Week.class);
        System.out.println(Week.MON.FRI.MON);
        System.out.println(Week.SUN.toLocaleString());
        
        for(int i=0;i<Week.values().length;i++){
            System.out.println("Week.values()["+i+"]:"+Week.values()[i]);
            System.out.println(Week.valueOf(Week.values()[i].toString()));
            System.out.println(Week.valueOf(Week.values()[i].toString()).toLocaleString());
        }
    }
}
原文地址:https://www.cnblogs.com/conncui/p/4272847.html