Proficiency in JDK1.5 enumeration type
In the previous article, the JDK1.5 new feature introduction, we discussed the simplest form of Enum, namely,
Public enum color {
RED,
Green,
Blue;
}
This article we describe in detail the features of Enum. Enum As a keyword of Sun's new introduction, it looks like a special Class, which can also have its own variable, which can define its own method, you can implement one or more interfaces. When we declare an enum type, we should notice that the ENUM type has some features.
1. It can't have a Public constructor, which guarantees that the customer code has no way to create an instance of an enum.
2. All enumeration values are public, static, final. Note that this is just for enumeration values, we can define any other type of non-enumerated variables as defining variables in a normal class, which can use any modifiers you want.
3. Enum The Java.lang.comParable interface is implemented by default.
4. ENUM covers the toString method, so we return the string "Blue" if we call Color.Blue.toString ().
5. ENUM provides a valueof method, this method and the toString method correspond to. Calling Valueof ("Blue") will return Color.Blue. So we should pay attention to this when you rewrite the toString method, and you should override the valueof method.
6. Enum also provides a Values method, which allows you to easily traverse all enumeration values.
7. ENUM has a method of Oridinal, this method returns an enumeration value in the order of enumeration class, which is set according to the order of enumeration values, where color.red.Rdinal () returns 0.
Understand these basic features, let's take a look at how to use them.
1. Traverse all have enumeration values. I know that there is a Values method, we can use the Foreach cycle with a light car to traverse the enumeration value.
For (Color C: color.values ())
System.out.println ("Find Value:" C);
2. Define methods and variables in Enum, such as we can add a method to Color Random Random Random.
Public enum color {
RED,
Green,
Blue;
/ *
* Defines a variable indicating the number of enumerated values.
* (I am a bit strange why Sun does not provide a size method directly to ENUM).
* /
Private static int number = color.values (). Length;
/ **
* Random return a enumeration value
@Return a Random Enum value.
* /
Public Static Color getrandomcolor () {
Long random = system.currenttimemillis ()% Number;
Switch (int) random) {
Case 0:
Return color.Red;
Case 1:
Return color.green;
Case 2:
Return color.blue;
Default: return color.red;
}
}
}
It can be seen that this defines variables and methods in the enumeration type and there is no difference between the definition method and variables in a normal class. The only thing to note is just that variables and method definitions must be placed behind all enumeration values definition, otherwise the compiler gives an error.
3. Override TOSTRING, VALUEOF method.
In front we know that Enum provides Tostring, Valueof, etc. In fact, this is nothing distinguished from the Tostring method that covers an ordinary Class. .
Public string toString () {
Switch (this) {
Case Red:
Return "color.red";
Case Green:
Return "color.green";
Case Blue:
Return "color.blue";
DEFAULT:
Return "Unknow Color";
}
}
.
At this time, we can see that when you use the front traversal code to print it out.
Color.Red
Color.green
Color.Blue
Instead of
Red
Green
Blue.
You can see that Tostring is indeed overloaded. Generally speaking, we should also override the Valueof method while covering toString to maintain their mutual consistency.
4. Use constructor.
Although ENUM does not have a PUBLIC's constructor, we can still define the constructor of the private, in the internal enum. Or use the color this example.
Public enum color {
RED ("this is red"),
Green ("this is green"),
Blue ("this is blue");
PRIVATE STRING DESC;
Color (String DESC) {
THIS.DESC = DESC;
}
Public string getdesc () {
Return this.desc;
}
}
Here we provide a description information for each color and then define a constructor to accept this explanation.
Be careful here that the constructor cannot be public or protected, so that the constructor can only be used inside, the customer code cannot get an instance of an enumeration value. This is also completely conforming to reason, because we know that the enumeration value is constant of public static final.
5. Implement a specific interface
We already know that enum can define variables and methods. It is also an interface to implement an interface, which is not an example.
6. Define enumeration values.
As we see if we can define some methods for Enum, we can even define methods for each enumerated value. In this way, the example of our previous overloading totring can be rewritten into this.
Public enum color {
RED {
Public string toString () {
Return "color.red";
}
}
Green {
Public string toString () {
Return "color.green";
}
}
Blue {
Public string toString () {
Return "color.blue";
}
}
}
Logically, this is clear than the original TOString method that originally provides a "global".
In general, Enum as a new type of new definition is that the code that can help programmers is more simple and easy. Personally do not need too much advanced features that use Enum, otherwise and easy to understand The original intention wants to violate it.
(The above code is running in Win2K 1.5.0-Beta2-B51)