Enumeration
first, enumeration type specific description
To put it simply. An enum is typically used to represent a set of constants of the same type.
such as gender, date, month, color, etc. The advantages of using constants for these properties are obvious, not only to guarantee the singleton, but also to be able to use "= =" to replace
Replace Equals. Before JDK1.5, there was no enum type, which was usually replaced by an interface constant.
Defining an enumeration type is essentially defining a category. Just a lot of detail by the compiler to help you finish. So, to some extent, enum is a keyword, and it acts like a class or a interface. (That's why some people can't find the value () method)
II. usage of enumeration types
1) Constants
Before an enum appears, we define constants that are usually: publicstatic fianl...?? This constant is more singular. The enumeration is much better, it can be seen as an object constant, and there is a similar set of functions, at the same time to define multiple object constants.
Such as:
<span style= "FONT-SIZE:14PX;" >enum Signal { GREEN, YELLOW, RED;} </span>
When used: Signal s = signal.red;
Note: Using the enumeration type with this scheme, the semicolon behind red can be used without writing
2) switch
<span style= "FONT-SIZE:14PX;" >public class TrafficLight { Signal color = signal.red; public void Change () {//traffic light toggle switch (color) {case RED: color = signal.green; Case YELLOW: color = signal.red; Case GREEN: color = Signal.yellow; break;}} </span>
3) Adding a new method to the enumeration
<span style= "FONT-SIZE:14PX;" >public enum Color {red ("yellow", 1), Green ("green", 2), BLANK ("White", 3), YELLOW ("yellowish", 4); Member variable private final String name; private final int index; The constructor method must be private to a Color (String name, int index) { this.name = name; This.index = index; } Public String GetName () {return name;} public int GetIndex () {return index;}} </span>
Because it is a constant, it is not possible for other people to be able to new this object. is to encapsulate this class. So that no one can be new. Method is by privatizing the construction method.Private Color (String name, int index)
Note: Assume that you intend to define your own method. Then a semicolon must be added at the end of the enum instance sequence.
That is, the yellow ("Yellow", 4) must be behind it; ”。
third, the static method of enumeration
There are two static methods for enumerating
1)values() Gets the full enumeration constant of the enumeration type with static?ElementType[] The return value of
For (Usertypeenum userType:UserTypeEnum.values ()) {<span style= "White-space:pre" ></span>if ( Usertype.getname (). Equalsignorecase (name)) {return Usertype.gettype ();}}
valueOf (class<t> enumtype, String name) return value is static? ElementType
Note: then I found a problem, the value method how I can not find, check to find. The values () method is the static method that the compiler inserts into the enum definition. So, when you convert an enum instance up to the parent enum class,
The values () are inaccessible.
Solution: In the Java.lang package under a java.lang.annotation bag (staring meaning), there is a ElementType class, in which there are two static methods you want to find.
So I can't say
The enumeration type essentially inherits the Java.lang.Enum class.
Four,Enumset class and Enummap class
These two classes are in a java.util package. look at the API, the main use should be no problem.
Java------Enumeration Types