Java enumeration introduction and usage

Source: Internet
Author: User

Java enumeration introduction and usage

In actual programming, there are often such "datasets", their values are stable in the program, and the elements in the "dataset" are limited.

For example, seven data elements from Monday to Sunday constitute a "dataset" for a week, and four data elements for spring, summer, and autumn constitute a "dataset" for the Four Seasons ".

How can we better use these "datasets" in java? Before jdk1.5, we may write as follows:

 

static class Grade {private Grade() {}public static final Grade A = new Grade();public static final Grade B = new Grade();public static final Grade C = new Grade();public static final Grade D = new Grade();public static final Grade E = new Grade();}

Private the constructor of the class, and obtain these datasets internally for external use. In fact, this write may cause other problems. In jdk1.5, enumeration occurs, which better solves this problem. The following describes how to use enumeration.

 

 

@ Testpublic void test () {print (Grade. a);} private void print (Grade grade) {System. out. println (grade. toString ();} // defines an enumeration class. Each enumeration value in it represents an object enum Grade {A, B, C, D, E ;}

In fact, an enumeration class is a special Java class, because when declaring an enumeration class, we can also declare attribute methods and constructors, but the constructors must be private. The following code is used:

 

 

@Testpublic void test() {print(Grade.A);}private void print(Grade grade) {System.out.println(grade.label);System.out.println(grade.getLabel());}enum Grade {A("100-91"), B("90-81"), C("80-71"), D("70-60"), E("60-0");private String label;private Grade(String label) {this.label = label;}private String getLabel(){return this.label;}}

Enumeration classes can also define abstract methods, but they must be implemented by enumeration values. They can also implement interfaces or inherit abstract classes, as shown below:

 

 

@ Testpublic void test () {print (Grade. b);} private void print (Grade grade) {System. out. println (grade. label); System. out. println (grade. getLabel (); System. out. println (grade. getCNLebal ();} enum Grade {A ("100-91") {@ Overridepublic String getCNLebal () {return "excellent ";}}, B ("90-81") {@ Overridepublic String getCNLebal () {return "good" ;}}, C ("80-71") {@ Overridepublic String getCNLebal () {return "normal" ;}}, D ("70-60") {@ Overridepublic String getCNLebal () {return "pass ";}}, E ("60-0") {@ Overridepublic String getCNLebal () {return "difference" ;}}; private String label; private Grade (String label) {this. label = label;} private String getLabel () {return this. label;} public abstract String getCNLebal ();}

Next, let's briefly introduce the common usage of the Eunm class. Refer to the Code:

 

 

@ Testpublic void test () {print (Grade. b); // Grade. values () obtains all enumerated values and returns an array System. out. println ("Number of score levels:" + Grade. values (). length); for (Grade grade: Grade. values () {String name = grade. name (); System. out. println ("when the enumerated Value name is:" + name); int ordinal = grade. ordinal (); System. out. println ("the coordinates of the current enumerated values are:" + ordinal);} // Grade. valueOf ("W") Grade. valueOf (Grade. class, "E") converts the current string into an enumeration. If it fails, an invalid parameter exception is thrown. Grade = grade. valueOf ("C"); print (grade); Grade grade2 = Grade. valueOf (Grade. class, "E"); print (grade2);} private void print (Grade grade) {System. out. println (grade. label); System. out. println (grade. getLabel (); System. out. println (grade. getCNLebal ();} enum Grade {A ("100-91") {@ Overridepublic String getCNLebal () {return "excellent ";}}, B ("90-81") {@ Overridepublic String getCNLebal () {return "good" ;}}, C ("80-71") {@ Overridepublic String getCNLebal () {return "normal" ;}}, D ("70-60") {@ Overridepublic String getCNLebal () {return "pass ";}}, E ("60-0") {@ Overridepublic String getCNLebal () {return "difference" ;}}; private String label; private Grade (String label) {this. label = label;} private String getLabel () {return this. label;} public abstract String getCNLebal ();}

In our program, enumeration can be used in a lot of places. The following uses a project case as an example. For example, we have an android online request client, communication between the two will define a lot of code and the corresponding message, so you can use enumeration in the program for simple implementation:

 

 

Public enum ServerCode {UNKNOWN (-1, "UNKNOWN error"), SUCCESS (0, "successful"), PARSE_ERROR (100, "returned value parsing error! "), NET_IO_ERROR (101," network communication error! "); Private int code = 0; private String message =" "; private ServerCode (int errorCode, String message) {this. code = errorCode; this. message = message;} public static ServerCode valueOf (int errorCode) {for (ServerCode: values () {if (code. code = errorCode) {return code;} return UNKNOWN;} public int code () {return code;} @ Override public String toString () {return message ;}

I believe everyone can understand the code above, so that many unnecessary operations can be omitted in the program. Of course, enumeration is not only used here, but there are many more.

 

After the common usage is completed, let's make a general summary:

 

The enumerated classes declared in Java are all children of the java. lang. Enum class and inherit all methods of the Enum class. Common Methods:
Name ()
Ordinal ()
Valueof ()
Values () traverses all enumeration values
Enumeration class features:
Enumeration class is a special java class.
Each declared enumerated value represents a powerful object of the enumeration class.
Similar to common classes in java, attribute methods and constructors can be declared when enumeration classes are declared. However, constructors in enumeration classes must be private
Enumeration classes can implement interfaces or inherit abstract classes
JDK 5 extends the switch statement to receive int byte char short enumeration.
If the enumeration class has only one enumerated value, it can be used as a standalone design mode.

 

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.