Use the enumeration class

 

First, the enumeration type

Enumerated type is part of the new features of Java 5, which is a special type of data, is special because it is a class (class) type but have become more special than the class type of constraint, but these constraints the presence also created the simplicity of enumerated types, safety and convenience. Here take a look at how to write an enumeration?

Second, the enumeration definition syntax

Define constants common way in the absence of an enumerated type

 

 

 The constants defined above is called int enumeration constants mode, such a definition way and there is nothing wrong, but it has many shortcomings, such as on the type of security and ease of use is not much benefit, if there is the same value as defined int variable, confusing, so this approach does not advocate after the enumeration appears, now we use to redefine the above enumerated type constants defined constants Monday to Sunday

// enumerated types, use the keyword enum

 

 

 Quite simple, keyword when defining enumeration type we are using the enum, and class keyword similar, but the former is defined enumerated types, which are defined class type.

 

Day enumerated types are defined in the value from Monday to Sunday, to note here, the value is generally uppercase letters, separated by a comma between multiple values. At the same time we should know that the enumeration type can be like class (class) types, defined as a single file, of course, can also be defined in other classes inside, more importantly enumeration constants in the type of security and convenience are guaranteed, if the types of problems arise compiler will prompt us to improve, but it is important to remember that the value represented enumeration type must be limited, which means that each value can be enumerated, such as described above There are seven days a week.

 

These are written, how to write this after using it? as follows:

 

 

 Like the above code as a direct reference to the value enumeration, this is the simplest model of an enumerated type.

 

Third, why can use this to see The principle enumeration know

We probably understand the definition of the enumeration type with simple use, it is necessary now to look at the basic realization of the principles enumerated type. In fact after the enum keyword to create enumerated types and the compiler, the compiler will generate a class relevant for us, this class inherits java.lang.Enum class in the Java API, that is to say by keyword to create pieces enum For type after compile is actually a class type and class inherits from java.lang.Enum class. We can look at the results decompiled!

Conclusion: From the decompiled code that the compiler can see really helped us generate a Day class and the class inherits from java.lang.Enum class, which is an abstract class, in addition, the compiler will also help us generate Day seven corresponding instance of the object type defined in the enumeration 7 date. We also generated for the two static methods, which are values ​​() and valueOf (), we will understand this, using the keyword enum defined enumerated type after compile, will also be converted into a real reality in the class, in this class, there will be corresponding to each instance of an object is defined the constants enumerated type, as described above enumerated type corresponding to MONDAY public static final Day MONDAY;

 

Four, Values ​​generated by the compiler method and the method ValueOf

values ​​() method and valueOf (String name) method is a static method generated by the compiler, analysis behind parent class Enum enum class of our own definition, it did not appear values ​​() method in Enum class, but valueOf ( ) method is still occurs, but the compiler generates valueOf method requires passing a parameter name (), while carrying the static method Enum valueOf () you need to pass two methods, it can be seen from the foregoing the decompiled code , valueOf methods generated by the compiler eventually calls the valueOf method Enum class to demonstrate the effect of the following two methods through code:

 

 

 Output:

 

 

 Known from the results, the role of values ​​() method is to acquire all the variables enumerated class, and returns as an array, acting valueOf (String name) valueOf method Enum method is similar to the class name acquisition enumeration, but valueOf method compiler generated more succinctly just pass a parameter.

 

Five, Enum abstract class common method

Enum is all Java language enumeration types common base class (note Enum is an abstract class), it is less common methods:

 

 

 ordinal()方法,该方法获取的是枚举变量在枚举类中声明的顺序,下标从0开始,如日期中的MONDAY在第一个位置,那么MONDAY的ordinal值就是0,如果MONDAY的声明位置发生变化,那么ordinal方法获取到的值也随之变化,注意在大多数情况下我们都不应该首先使用该方法,毕竟它总是变幻莫测的。

 

compareTo(E o)方法则是比较枚举的大小,注意其内部实现是根据每个枚举的ordinal值大小进行比较的。

 

name()方法与toString()几乎是等同的,都是输出变量的字符串形式。

 

valueOf(Class enumType, String name)方法则是根据枚举类的Class对象和枚举名称获取枚举常量,注意该方法是静态的。

 

下面的代码演示了上述方法:

 

 

 

 

 

 

六、枚举的进阶用法

向enum类添加方法与自定义属性和构造函数 重新定义一个日期枚举类,带有desc成员变量描述该日期的对于中文描述,同时定义一个getDesc方法,返回中文描述内容,自定义私有构造函数,在声明枚举实例时传入对应的中文描述,代码如下:

 

 

 

 输出结果:

————————————————

版权声明:本文为CSDN博主「奋斗的哼哼」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_43610698/article/details/90737759

 

 

一、理解枚举类型枚举类型是Java 5中新增特性的一部分,它是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁性、安全性以及便捷性。下面先来看看如何写一个枚举?二、枚举的定义语法在没有枚举类型时定义常量常见的方式    public class DayDemo {        public static final int MONDAY =1;        public static final int TUESDAY=2;        public static final int WEDNESDAY=3;        public static final int THURSDAY=4;        public static final int FRIDAY=5;        public static final int SATURDAY=6;        public static final int SUNDAY=7;    }123456789上述的常量定义常量的方式称为int枚举模式,这样的定义方式并没有什么错,但它存在许多不足,如在类型安全和使用方便性上并没有多少好处,如果存在定义int值相同的变量,容易混淆,因此这种方式在枚举出现后并不提倡,现在我们利用枚举类型来重新定义上述的常量,定义周一到周日的常量//枚举类型,使用关键字enum
    enum Day {        MONDAY, TUESDAY, WEDNESDAY,        THURSDAY, FRIDAY, SATURDAY, SUNDAY    }1234相当简洁,在定义枚举类型时我们使用的关键字是enum,与class关键字类似,只不过前者是定义枚举类型,后者是定义类类型。
枚举类型Day中分别定义了从周一到周日的值,这里要注意,值一般是大写的字母,多个值之间以逗号分隔。同时我们应该知道的是枚举类型可以像类(class)类型一样,定义为一个单独的文件,当然也可以定义在其他类内部,更重要的是枚举常量在类型安全性和便捷性都很有保证,如果出现类型问题编译器也会提示我们改进,但务必记住枚举表示的类型其取值是必须有限的,也就是说每个值都是可以枚举出来的,比如上述描述的一周共有七天。
以上是写法,写好后该如何使用呢?如下:
    public class EnumDemo {        public static void main(String[] args){            //直接引用            Day day =Day.MONDAY;        }    }123456就像上述代码那样,直接引用枚举的值即可,这便是枚举类型的最简单模型。
三、为什么可以这样用,看看枚举实现原理就知道了我们大概了解了枚举类型的定义与简单使用后,现在有必要来了解一下枚举类型的基本实现原理。实际上在使用关键字enum创建枚举类型并编译后,编译器会为我们生成一个相关的类,这个类继承了Java API中的java.lang.Enum类,也就是说通过关键字enum创建枚举类型在编译后事实上也是一个类类型而且该类继承自java.lang.Enum类。我们可以看看反编译的结果!结论:从反编译的代码可以看出编译器确实帮助我们生成了一个Day类而且该类继承自java.lang.Enum类,该类是一个抽象类,除此之外,编译器还帮助我们生成了7个Day类型的实例对象分别对应枚举中定义的7个日期。还为我们生成了两个静态方法,分别是values()和 valueOf(),到此我们也就明白了,使用关键字enum定义的枚举类型,在编译期后,也将转换成为一个实实在在的类,而在该类中,会存在每个在枚举类型中定义好常量的对应实例对象,如上述的MONDAY枚举类型对应public static final Day MONDAY;
四、编译器生成的Values方法与ValueOf方法values()方法和valueOf(String name)方法是编译器生成的static方法,后面我们自己定义的枚举类的父类Enum的分析中,在Enum类中并没出现values()方法,但valueOf()方法还是有出现的,只不过编译器生成的valueOf()方法需传递一个name参数,而Enum自带的静态方法valueOf()则需要传递两个方法,从前面反编译后的代码可以看出,编译器生成的valueOf方法最终还是调用了Enum类的valueOf方法,下面通过代码来演示这两个方法的作用:    Day[] days2 = Day.values();    System.out.println("day2:"+Arrays.toString(days2));    Day day = Day.valueOf("MONDAY");    System.out.println("day:"+day);1234输出结果:
  day2:[MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]  day:MONDAY12从结果可知道,values()方法的作用就是获取枚举类中的所有变量,并作为数组返回,而valueOf(String name)方法与Enum类中的valueOf方法的作用类似根据名称获取枚举变量,只不过编译器生成的valueOf方法更简洁些只需传递一个参数。
五、Enum抽象类常见方法Enum是所有 Java 语言枚举类型的公共基本类(注意Enum是抽象类),以下是它的常见方法:

ordinal()方法,该方法获取的是枚举变量在枚举类中声明的顺序,下标从0开始,如日期中的MONDAY在第一个位置,那么MONDAY的ordinal值就是0,如果MONDAY的声明位置发生变化,那么ordinal方法获取到的值也随之变化,注意在大多数情况下我们都不应该首先使用该方法,毕竟它总是变幻莫测的。
compareTo(E o)方法则是比较枚举的大小,注意其内部实现是根据每个枚举的ordinal值大小进行比较的。
name()方法与toString()几乎是等同的,都是输出变量的字符串形式。
valueOf(Class enumType, String name)方法则是根据枚举类的Class对象和枚举名称获取枚举常量,注意该方法是静态的。
下面的代码演示了上述方法:
    public class EnumDemo {        public static void main(String[] args){            //创建枚举数组            Day[] days=new Day[]{Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY,                    Day.THURSDAY, Day.FRIDAY, Day.SATURDAY, Day.SUNDAY};        for (int i = 0; i <days.length ; i++) {            System.out.println("day["+i+"].ordinal():"+days[i].ordinal());        }
        System.out.println("-------------------------------------");        //通过compareTo方法比较,实际上其内部是通过ordinal()值比较的        System.out.println("days[0].compareTo(days[1]):"+days[0].compareTo(days[1]));        System.out.println("days[0].compareTo(days[1]):"+days[0].compareTo(days[2]));
        //获取该枚举对象的Class对象引用,当然也可以通过getClass方法        Class<?> clazz = days[0].getDeclaringClass();        System.out.println("clazz:"+clazz);
        System.out.println("-------------------------------------");
        //name()        System.out.println("days[0].name():"+days[0].name());        System.out.println("days[1].name():"+days[1].name());
        System.out.println("-------------------------------------");
        System.out.println("days[0].toString():"+days[0].toString());        System.out.println("days[1].toString():"+days[1].toString());               System.out.println("-------------------------------------");
        Day d=Enum.valueOf(Day.class,days[0].name());        Day d2=Day.valueOf(Day.class,days[0].name());        System.out.println("d:"+d);        System.out.println("d2:"+d2);    }123456789101112131415161718192021222324252627282930313233343536六、枚举的进阶用法向enum类添加方法与自定义属性和构造函数 重新定义一个日期枚举类,带有desc成员变量描述该日期的对于中文描述,同时定义一个getDesc方法,返回中文描述内容,自定义私有构造函数,在声明枚举实例时传入对应的中文描述,代码如下:    public enum Day2 {        MONDAY("星期一",1),        TUESDAY("星期二",2),        WEDNESDAY("星期三",3),        THURSDAY("星期四",4),        FRIDAY("星期五",5),        SATURDAY("星期六",6),        SUNDAY("星期日",7);//记住要用分号结束
    private String desc;//文字描述    private Integer code; //对应的代码
    /**     * 私有构造,防止被外部调用     * @param desc     */    private Day2(String desc,Integer code){        this.desc=desc;     this.code=code;        }    /**     * 定义方法,返回描述,跟常规类的定义没区别     * @return     */    public String getDesc(){        return desc;    }
     /**         * 定义方法,返回代码,跟常规类的定义没区别         * @return         */        public String getCode(){            return code;        }    public static void main(String[] args){        for (Day2 day:Day2.values()) {            System.out.println("name:"+day.name()+                    ",desc:"+day.getDesc());        }    }1234567891011121314151617181920212223242526272829303132333435363738394041输出结果:
  name:MONDAY,desc:星期一  name:TUESDAY,desc:星期二  name:WEDNESDAY,desc:星期三  name:THURSDAY,desc:星期四  name:FRIDAY,desc:星期五  name:SATURDAY,desc:星期六  name:SUNDAY,desc:星期日————————————————版权声明:本文为CSDN博主「奋斗的哼哼」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/weixin_43610698/article/details/90737759

Guess you like

Origin www.cnblogs.com/morganlin/p/12122935.html