On the function and benefit of Jave enumeration _java

Source: Internet
Author: User

An enumeration is a specification that regulates the form of a parameter so that you do not have to consider the type mismatch and explicitly override the fuzzy concepts that an int parameter might bring, like a class, or an array.

As a new keyword introduced by Sun, an enum looks like a special class, it can have its own variables, you can define its own method, and you can implement one or more interfaces. When we declare an enum type, we should notice some of the characteristics of the enum type.

1.it cannot have the public constructor, this ensures that the client code has no way to create an instance of an enum.

2.all enumerated values are public, static, final。 Note that this is only for enumeration values, and we can define any other type of non enumerated variable, as you would define variables in the normal class, with any modifiers you want to use.

3. The enum defaults to implement the Java.lang.Comparable interface.

4. The enum repeats the ToString method, so if we call Color.Blue.toString () The default return string "Blue".

5. The enum provides a valueof method, which corresponds to the ToString method. Calling valueof ("Blue") returns Color.Blue. So we need to be aware of this when we rewrite the ToString method ourselves, and one should rewrite the valueof method correspondingly.

6. The enum also provides the values method, which allows you to traverse all enumerated values conveniently.

7. An enum also has a oridinal method that returns the order of enumerated values in the enumeration class, which is based on the order in which the enumeration values are declared, where Color.Red.ordinal () returns 0.

With these basic features in view, let's look at how to use them.

1. Iterate through all the enumerated values. Knowing the values method, we can traverse the enumeration value with a Foreach loop.

For (Color c:color.values ())
System.out.println ("Find value:" + C);

2. Define methods and variables in the enum, such as we can add a method for color to randomly return a color.

public enum Color { 
Red, 
Green, 
Blue; 


private static int number = Color.values (). length; 


public static Color Getrandomcolor () { 
long random = System.currenttimemillis ()%; 
switch ((int) random) {case 
0: return 
color.red; 
Case 1: Return 
color.green; 
Case 2: Return 
Color.Blue; 
Default:return color.red 
}} 
} 
 

You can see that there is no difference between defining variables and methods in an enumeration type and defining methods and variables within a normal class. The only thing to note is that the variables and method definitions must be placed after all the enumeration value definitions, otherwise the compiler will give an error.

3. (Override) toString, valueof method

We already know that Enum provides tostring,valueof, and many times we need to override the default ToString method, so what do we do with enums? There is no difference between this and the ToString method of loading a normal 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 point we can see that the previous traversal code is used to print the

Color.Red
Color.green
Color.Blue

Instead of

Red
Green
Blue.

It can be seen that ToString is indeed covered by the load. In general, we should also cover the valueof method at the time of the loading of ToString, in order to maintain their mutual consistency.

4. Using constructors

Although an enum may not have a public constructor, we can still define the private constructor, which is used inside the enum. Or use the example of color.

public enum Color { 
red (' This is Red '), 
Green ("This Is Green"), 
Blue (' is blue '); 

Private String desc; 

Color (String desc) { 
This.desc = 
} public 

String GetDesc () {return 
this.desc; 
} 

Here we provide a descriptive information for each color and then define a constructor to accept this descriptive information.

Note that the constructor cannot be public or protected, so that the constructor can only be used internally, and the client code cannot be a new instance of an enumerated value. This is perfectly reasonable, because we know that the enumeration value is a constant of public static final.

5. Implementing a specific interface

We already know that an enum can define a variable and a method, and that it implements an interface as well as an interface with normal class, which is not an example here.

6. Define your own method of enumeration values.

We saw earlier that we could define some methods for an enum, but we could even define a method for each enumerated value. In this way, we can rewrite the example of ToString in front of it.

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 a bit clearer than the original ToString method of providing a "global".

In general, the enum as a completely new definition of the type, is to be able to help programmers write code more easily understandable, a

People feel that there is generally no need to use some of the advanced features of the enum, otherwise, and easy to understand the original intention to violate.

The above article discusses the function and the advantage of the Jave enumeration is to share all the content of the small, hope to give you a reference, but also hope that we support the cloud habitat community.

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.