Use and Analysis of Enum and use analysis of Enum

Source: Internet
Author: User

Use and Analysis of Enum and use analysis of Enum

Example:

package test;public enum ColorEnum {    RED,    YELLOW,    BLUE,    GREEN,    BLACK,;}

Obviously, enum is similar to a special class. In fact, the type defined by enum is a class. These classes are subclasses of The Enum class in the class library (java. lang. Enum <E> ). They inherit many useful methods in this Enum. After compiling the code, we found that the compiler compiled the enum type into a bytecode file: Color. class.

package test;public final class ColorEnum extends Enum{    public static ColorEnum[] values()    {        return (ColorEnum[])$VALUES.clone();    }    public static ColorEnum valueOf(String s)    {        return (ColorEnum)Enum.valueOf(test/ColorEnum, s);    }    private ColorEnum(String s, int i)    {        super(s, i);    }    public static final ColorEnum RED;    public static final ColorEnum YELLOW;    public static final ColorEnum BLUE;    public static final ColorEnum GREEN;    public static final ColorEnum BLACK;    private static final ColorEnum $VALUES[];    static     {        RED = new ColorEnum("RED", 0);        YELLOW = new ColorEnum("YELLOW", 1);        BLUE = new ColorEnum("BLUE", 2);        GREEN = new ColorEnum("GREEN", 3);        BLACK = new ColorEnum("BLACK", 4);        $VALUES = (new ColorEnum[] {            RED, YELLOW, BLUE, GREEN, BLACK        });    }}

The features and usage of enumeration classes defined by enum are described in detail below. (Color is used later)

1. The Color enumeration class is a class which cannot be inherited. The enumerated values (RED, BLUE...) are static constants of the Color type. We can use the following method to obtain an instance of the Color enumeration class:
Color c = Color. RED;
Note: These enumerated values are all public static final, that is, the constant method that we often define. Therefore, it is recommended that all enumeration values in the enumeration class be capitalized.

2. The enumeration class is class. Of course, there are constructors, methods, and data fields in the enumeration type. However, the constructors of enumeration classes are very different:
(1) The constructor is called only when constructing enumeration values.

1 package test; 2 3 public enum ColorEnum {4 RED (255, 0, 0), BLUE (0, 0,255), BLACK (0, 0, 0), YELLOW (255,255, 0), GREEN (0,255, 0); 5 6 // construct enumeration values, such as RED (, 0, 0) 7 private ColorEnum (int rv, int gv, int bv) {8 this. redValue = rv; 9 this. greenValue = gv; 10 this. blueValue = bv; 11} 12 13 public String toString () {// overwrite the toString () 14 return super of the parent class Enum. toString () + "(" + redValue + "," + greenValue + "," + BlueValue + ")"; 15} 16 17 private int redValue; // custom data domain, private for encapsulation. 18 private int greenValue; 19 private int blueValue; 20}

(2) The constructor can only be private. It is absolutely not allowed to have a public constructor. This prevents external code from creating new enumeration class instances. This is also reasonable because we know that the enumerated value is a constant of public static final. However, enumeration methods and data domains allow external access.

Public static void main (String args []) {// Color colors = new Color (100,200,300); // wrong Color color = Color. RED; System. out. println (color); // The toString () method is called}

3. All enumeration classes inherit the Enum method. The following describes these methods in detail.
(1) ordinal () method: return the order of the enumerated values in the enumerated classes. This order depends on the order in which enumeration values are declared.
ColorEnum. RED. ordinal (); // The returned result is 0.
ColorEnum. BLUE. ordinal (); // The returned result is 1.
(2) compareTo () method: Enum implements the java. lang. Comparable interface, so it can be compared to the order of the specified object. CompareTo in Enum returns the order difference between the two enumerated values. Of course, the premise is that the two enumerated values must belong to the same enumeration class, otherwise the ClassCastException () exception will be thrown. (Specific visible source code)
ColorEnum. RED. compareTo (Color. BLUE); // return result-1
(3) values () method: static method. An array containing all enumerated values is returned.
ColorEnum [] colors = ColorEnum. values ();
For (ColorEnumc: colors ){
System. out. print (c + ",");
} // Return result: RED, BLUE, black yellow, GREEN,
(4) toString () method: returns the name of the enumerated constant.
ColorEnum c = ColorEnum. RED;
System. out. println (c); // The returned result is RED.
(5) valueOf () method: This method corresponds to the toString method, and an enumerated constant of the specified enumeration type with the specified name is returned.
ColorEnum. valueOf ("BLUE"); // return result: ColorEnum. BLUE
(6) equals () method: Compares the references of two enumeration class objects.

// JDK source code: public final boolean equals (Object other) {return this = other ;}

4. Enumeration classes can be used in switch statements.

ColorEnum color = ColorEnum .RED;  switch(color){          case RED: System.out.println("it's red");break;          case BLUE: System.out.println("it's blue");break;          case BLACK: System.out.println("it's blue");break;  }  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.