枚举的使用及优势

来源:互联网 发布:mac 恢复删除文件 编辑:程序博客网 时间:2024/05/19 04:04

   枚举是JDK1.5引入的新特性,存放在java.lang包中,隐含了所创建的枚举类都是java.lang.Enum类的子类,枚举类型符合通用class Enum<E extends Enum<E>>,E表示枚举类的名称,枚举类型的每一个值都将映射到protected Enum(String name,int ordien)构造函数中,在这里每个值的名称 都被转成了字符串,下面将从以下几点说明枚举的使用和优势

   1.定义一个枚举类需要使用关键字enum

 public enum TypeValues {    SMALL, MIDDLE, LARGE }

  实际上隐含的调用了

  new Enum<TypeValus>("SMALL",0);

  new Enum<TypeValus>("MIDDLE",1);

  new Enum<TypeValus>("LARGF",2);

  

   2.使用values()方法获取枚举中的值,values()方法返回枚举类中枚举类型的个数

 for (TypeValues e : TypeValues.values()) {      System.out.print(e.toString()); }
   

   3.枚举的常用方法

 /**  * Returns the ordinal of this enumeration constant (its position  * in its enum declaration, where the initial constant is assigned  * an ordinal of zero).  *  * Most programmers will have no use for this method.  It is  * designed for use by sophisticated enum-based data structures, such  * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.  *  * @return the ordinal of this enumeration constant */ public final int ordinal() {    return ordinal; }
返回枚举常量的位置,和创建的位置相同,第一个位置为0


 /**  * Returns the name of this enum constant, exactly as declared in its  * enum declaration.  *  * <b>Most programmers should use the {@link #toString} method in  * preference to this one, as the toString method may return  * a more user-friendly name.</b>  This method is designed primarily for  * use in specialized situations where correctness depends on getting the  * exact name, which will not vary from release to release.  *  * @return the name of this enum constant */ public final String name() {    return name; }
返回枚举常量的名称


 /**  * Returns the Class object corresponding to this enum constant's  * enum type.  Two enum constants e1 and  e2 are of the  * same enum type if and only if  *   e1.getDeclaringClass() == e2.getDeclaringClass().  * (The value returned by this method may differ from the one returned  * by the {@link Object#getClass} method for enum constants with  * constant-specific class bodies.)  *  * @return the Class object corresponding to this enum constant's  *     enum type  */ public final Class<E> getDeclaringClass() {    Class clazz = getClass();    Class zuper = clazz.getSuperclass();    return (zuper == Enum.class) ? clazz : zuper; }
获取当前枚举常量所在的Class对象


 /**  * Compares this enum with the specified object for order.  Returns a  * negative integer, zero, or a positive integer as this object is less  * than, equal to, or greater than the specified object.  *  * Enum constants are only comparable to other enum constants of the  * same enum type.  The natural order implemented by this  * method is the order in which the constants are declared.  */ 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; }

比较枚举类中枚举常量的顺序


   4.enum类虽然和class语法不太一样,但是经过编译器编译之后产生的是一个class文件,该class文件经过反编译可以看到实际上生成了一个类,该类继承了java.lang.Enum<E>,所以实际上enum就是一个类,只不过Java编译器帮我们做了语法的解析和编译。

 public class com.util.TypeValues extends java.lang.Enum{   public static final com.util.TypeValues MON;   public static final com.util.TypeValues TUE;   public static final com.util.TypeValues WED;}

  

   5.使用枚举的优势

     限定了类型的范围

 private int type; private TypeValues typeValues; public void setType(int type) {    this.type = type; } public void setTypeValues(TypeValues typeValues) {    this.typeValues = typeValues; }
 这里的set()方法一个传入的是int类型,一个传入的是枚举类型,int类型取值太广泛了,而枚举值只能取TypeValues.SMALL、TypeValues.MIDDLE、TypeValus.LARGE,相比较而言枚举值是不是用起来,相对规范点。

   

原创粉丝点击