Java Enum Enumeration Details

In some cases, the object of a class is finite and fixed. For example, the seasonal class has only four attributes. Classes with limited and fixed instances like this are called enumeration classes. Yes, it's still a class, with all the features of the class.
Let's start by creating an enumeration class:

**

  • Failure to create enumeration file

**

new>Enum
Alas, it turns out that you can't create it when you create it (if you use eclipse), as follows:

This is because Eclipse can specify compilation depth with a default value of 1.4 and enumeration occurs after jdk1.5. Need to be modified: Project > proterties > Java Compiler. Then modify it, as shown in the figure:

Then apply ok.

Next, create a Colo enumeration class with three instance objects

public enum Color {
    RED,GREEN,BLUE;
}

There are three entity class objects in the enumeration class.
1. And they can only be written on the first line, otherwise these entity objects are invalid.
2. Enumeration classes inherit Enum classes and cannot be shown to inherit other classes. Enum classes implement Serializable and Comparable interfaces.
3. If the enumeration class is non-abstract, it cannot be inherited.

  • The switch statement also provides support for enumeration classes:
public class Test {
    public static void main(String[] args) {
        //Call the test method and pass the BLUE object
        test(Color.BLUE);
    }
    /**
     * 
     * @param c Enumerating class objects
     */
    public static void test(Color c) {
        switch (c) {
        case RED:
            System.out.println("red");
            break;
        case BLUE:
            System.out.println("blue");
            break;
        case GREEN:
            System.out.println("green");
            break;
        default:
            break;
        }
    }

}

As you can see, in the switch statement, the object name can be written directly without being named by the class name.
**

  • Enumeration classes are also classes

**
Enumeration class is also a kind of class, which can also have member variables, methods, construction methods.

  • Enumerate member variables in a class:
public enum Color {
    RED,GREEN,BLUE;
    public String name;
}

Use:

public class Test {
    public static void main(String[] args) {
        Color.BLUE.name="blue";
        System.out.println(Color.BLUE.name);
    }

}

Output: Blue
It is no different from the usage of ordinary class objects.

  • Membership methods:
public enum Color {
    //Three entity class objects
    RED,GREEN,BLUE;
    //Membership variables
    public String name;

    /**
     * Membership method
     * @param str
     */
    public void eat(String str){
        System.out.println(str);
    }
}

Call:

public class Test {
    public static void main(String[] args) {
        Color.BLUE.eat("Not to eat");
    }

}
  • Construction method
public enum Color {
    //Three entity class objects, the enumerated values here, must be created by calling the constructor (because there is only one constructor)
    RED("gules"),GREEN("green"),BLUE("blue");
    //Membership variables
    public String name;
    //Parametric construction method
    private Color(String name) {
        this.name = name;
    }

    /**
     * Membership method
     * @param str
     */
    public void eat(String str){
        System.out.println(str);
    }
}

Like ordinary classes, only one constructor can be used to write any constructor.

  • Enumeration class implementation interface
public enum Color implements Runnable{
    //Three entity class objects, the enumerated values here, must be created by calling the constructor (because there is only one constructor)
    RED,GREEN,BLUE;
//Method of Rewriting Interface
    @Override
    public void run() {


    }
}

As you can see, there is no difference from inheritance of ordinary classes, but each object performs the same rewriting method and does the same thing. In many cases, our needs are different objects, processing different data, it is necessary to rewrite these methods separately.

public enum Color implements Runnable{
    //This is an object obtained from an anonymous inner class
    RED{

        @Override
        public void run() {
            System.out.println("Red light");

        }

    }
    ,GREEN{

        @Override
        public void run() {
            System.out.println("Green light");

        }

    }
    ,BLUE{

        @Override
        public void run() {
            System.out.println("Blu ray");

        }

    };
}

At this time, the enumeration class Color does not override the abstract method of the interface, so Color is an abstract enumeration class, which can obtain concrete instances through subclass objects.

Call:

public class Test {
    public static void main(String[] args) {
        Color.BLUE.run();
    }

}

Output: blue light

EnumSet and EnumMap are the collections dedicated to storing enumerated entity class objects.
In short, enumerations are classes. It's like a square is a rectangle.

Keywords: Eclipse Java

Added by dreamdelerium on Sat, 22 Jun 2019 23:16:09 +0300