码迷,mamicode.com
首页 > 编程语言 > 详细

java枚举类型

时间:2017-08-19 22:14:00      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:nali   lan   结果   ack   des   static   java   odata   用法   

jvm并不支持枚举类型,java中枚举类型是在编译器层面上实现的,先看如下代码:

package demo.nio;

public class EnumDemo {

    public static enum Color{
        Red(20),
        Green(10),
        Blue(30);
        
        Color(int v){
            this.value = v;
        }
        
        private int value;
        public int getValue(){
            return this.value;
        }
    }
    
    public static void main(String[] args) {
        System.out.println(Color.class.getSuperclass());
    }

}

输出结果:class java.lang.Enum

也就是说所有的枚举类型都是 java.lang.Enum的直接子类,java.lang.Enum是个抽象类,内部两个字段,ordinal和name,代码如下:

技术分享
package java.lang;

import java.io.Serializable;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;

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 self = this;
        if (self.getClass() != other.getClass() &&self.getDeclaringClass() != other.getDeclaringClass())
            throw new ClassCastException();
        return self.ordinal - other.ordinal;
    }

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

从代码可以看出,此类不可以实例化。compareTo,是ordinal的比较。一个枚举类型的实现就相当于如下代码:

public class Color extends java.lang.Enum{
        public static Color Red = new Color("Red",0);
        public static Color Green = new Color("Green",1);
        public static Color Blue = new Color("Blue",2);
        public Color(String name,int ordinal){
            super(name,ordinal);
        }
    }

当时编译器并不允许这样显示定义,当时用法是完全是上述定义相符合的

public static void main(String[] args) {
        System.out.println(Color.class.getSuperclass());
        System.out.println(Color.Blue.ordinal());
        System.out.println(Color.Blue.name());
    }
    

输出结果:

class java.lang.Enum
2
Blue

如果枚举中包含字段和方法,方法和容易理解,只要是该类型的对象都可以调用该方法,字段和字段的初始化也并不复杂,假如枚举中包含一个int型的value。如上面的Color定义:

技术分享
public class Color extends java.lang.Enum{
        public static Color Red = new Color("Red",0);
        public static Color Green = new Color("Green",1);
        public static Color Blue = new Color("Blue",2);
        public Color(String name,int ordinal){
            super(name,ordinal);
        }
        
        public Color(String name,int ordinal,int value){
            super(name,ordinal);
            this.value = value;
        }
        
        public int value;
    }
View Code

除此之外,Color还包含了一些静态方法,valueOf用于根据名称获取该对象,values获取所有该类型的对象示例如下:

技术分享
public static void main(String[] args) {
        System.out.println(Color.class.getSuperclass());
        for(Color c:Color.values()){
            System.out.println(c.name());
        }
        System.out.println(Color.valueOf("Blue"));
        System.out.println(Enum.valueOf(Color.class,"Blue"));
    }
View Code

输出结果如下:

class java.lang.Enum
Red
Green
Blue
Blue
Blue

 

java枚举类型

标签:nali   lan   结果   ack   des   static   java   odata   用法   

原文地址:http://www.cnblogs.com/yangyang12138/p/7397822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!