• 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	 ordinal	 compareTo(E)	 equals		x ==E");
            for (Const c : Const.values()) {
                System.out.println(c.name() + "	 " + c.ordinal() + "		" + c.compareTo(Const.E)
                                   + "		" + c.equals(Const.E) + "		" + (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
    
  • 相关阅读:
    面试题63 二叉搜索树的第k个结点
    面试题62 序列化二叉树
    面试题61 把二叉树打印成多行
    面试题60 按之字形顺序打印二叉树
    centos下Nginx代理访问web服务
    回文数
    两数之和--哈希表unordered_map
    删除链表的倒数第N个节点---链表的应用
    基础篇,排序(冒泡排序,快速排序)
    linuxC网络聊天室简单代码,CSDN博客迁移
  • 原文地址:https://www.cnblogs.com/wei-zw/p/8797798.html
Copyright © 2020-2023  润新知