Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Java, what is enumeration?
Java, what is enumeration?
As a new keyword introduced by Sun, Enum looks like a special class. It can also have its own variables, define its own methods and implement one or more interfaces. When we declare an enumeration type, we should notice that the enumeration type has the following characteristics.

1. It cannot have a public constructor, which ensures that client code cannot create a new instance of enum.

2. All enumeration values are public, static and final. Note that this applies only to enumerated values. We can define any other type of non-enumerated variables just like we define variables in ordinary classes, and these variables can use any modifiers you want.

3. By default, enum implements the java.lang.Comparable interface.

4.enum rewrites the toString method, so if we call Color. Blue.toString (), which returns the string "blue" by default.

5.enum provides the valueOf method, which corresponds to the toString method. Calling valueOf("Blue ") will return Color. Blue So we should pay attention to this when rewriting the toString method ourselves, and rewrite the valueOf method accordingly.

6.enum also provides the values method, which enables you to easily traverse all enumerated values.

7.enum also has an oridinal method, which returns the order of enumeration values in the enumeration class, which depends on the order in which the enumeration values are declared, where Color. Red.ordinal () returns 0.

1. Iterates through all enumerated values. Knowing the values method, we can skillfully use the ForEach loop to traverse the enumerated values.

for (Color c: Color.values())

System.out.println ("lookup value:"+c);

2. Define methods and variables in enumeration. For example, we can add a method to return Color randomly for color.

Common enumeration color {

Red,

Green,

Blue;

Private static int number = Color.values (). Length;

Public static color getRandomColor(){

long random = system . current time millis()% number;

Switch ((int) random) (

Case 0:

Returns the color. Red;

Case 1:

Returns the color. Green;

Case 2:

Returns the color. Blue;

Default: Return color. Red;

}

}

}

It can be seen that defining variables and methods in enumerated types is no different from defining methods and variables in ordinary classes. The only thing to note is that variable and method definitions must be placed after all enumeration value definitions, otherwise the compiler will give an error.

3.Override)toString, valueOf method.

We already know that enum provides methods such as toString and valueOf, and many times we need to override the default toString method, so what do we do with enum? In fact, this is no different from the toString method that covers ordinary classes.

….

Common string toString(){

Switch (this) {

Red shell:

Return to Color. Red ";

Green shell:

Return to Color. Green ";

Blue shell:

Return to Color. Blue ";

Default value:

Returns "Unknown color";

}

}

….

At this time, we can see that what is printed with the previous traversal code is

Color. red

Color. Green (surname); green

Color. blue

rather than

red

Green (surname); green

Blue.

You can see that toString is really overloaded. Generally speaking, when rewriting toString, we should also rewrite the valueOf method to make them consistent.

4. Use the constructor.

Although enum cannot have a public constructor, we can still define a private constructor for use inside enum. Let's take color as an example.

Common enumeration color {

Red ("this is red"),

Green ("this is green"),

Blue ("this is blue");

Private string desc;;

Color (string desc) (

this . desc = desc;

}

Common string getDesc(){

Return this.desc

}

}

Here we provide a description information for each color.

Common enumeration color {

Red {

Common string toString(){

Return to Color. Red ";

}

},

Green {

Common string toString(){

Return to Color. Green ";

}

},

Blue {

Common string toString(){

Return to Color. Blue ";

}

};

}

Then define a constructor to accept this description information.

Note that the constructor here cannot be public or protected, so as to ensure that the constructor can only be used internally, and the client code cannot create instances of enumerated values. This is also completely reasonable, because we know that enumeration values are constants of public static final.

5. Implement a specific interface

We already know that enum can define variables and methods, and its implementation of an interface is the same as that of ordinary classes, so I won't give an example here.

6. Define your own method of enumerating values.

As we saw earlier, we can define some methods for enum. In fact, we can even define methods for each enumerated value. In this way, our previous example of rewriting toString can be rewritten like this.

Logically, this is clearer than the original "global" toString method.

Generally speaking, enum, as a newly defined type, hopes to help programmers write more simple and understandable codes. Personally, there is no need to use some advanced features of enum too much, otherwise it will violate the original intention of being easy to understand.