Java enumeration 7 common usage

Source: Internet
Author: User

Dk1.5A new type-enumeration is introduced. InJavaAlthough it is a "small" function, it brings great convenience to my development.

Usage 1: Constant

InJdk1.5Previously, we defined constants:Public static fianl ..... Now, with enumeration, You can group related constants into one Enumeration type, and enumeration provides more methods than constants.

 
Public Enum color {red, green, blank, yellow}

Usage 2: Switch

The switch statement before jdk1.6 only supports int, Char, and enum types.CodeMore readable.

Enum signal {green, yellow, red} public class trafficlight {signal color = signal. red; Public void change () {Switch (color) {Case RED: color = signal. green; break; Case Yellow: color = signal. red; break; Case Green: color = signal. yellow; break ;}}}

Usage 3: Add a new method to Enumeration

If you want to customize your own method, you mustAdd a semicolon to the end of the Instance sequence. AndJavaMust be defined firstEnumInstance.

Public Enum color {red ("red", 1), Green ("green", 2), blank ("white", 3), yello ("yellow", 4 ); // member variable private string name; private int index; // constructor private color (string name, int index) {This. name = Name; this. index = index;} // common method public static string getname (INT index) {for (color C: color. values () {If (C. getindex () = index) {return C. name ;}} return NULL;} // Get Set Method Public String getname () {return name;} public void setname (string name) {This. name = Name;} public int getindex () {Return Index;} public void setindex (INT index) {This. index = index ;}}

Usage 4: override Enumeration Method

The following is an example of overwriting the tostring () method.

Public Enum color {red ("red", 1), Green ("green", 2), blank ("white", 3), yello ("yellow", 4 ); // member variable private string name; private int index; // constructor private color (string name, int index) {This. name = Name; this. index = index;} // override method @ overridepublic string tostring () {return this. index + "_" + this. name ;}}

Usage 5: Implementation Interface

All enumerations are inherited from the java. Lang. Enum class. BecauseJavaMulti-inheritance is not supported, so enumeration objects cannot inherit other classes.

Public Interface Behaviour {void print (); string getinfo ();} public Enum color implements behaviour {red ("red", 1), Green ("green", 2 ), blank ("white", 3), yello ("yellow", 4); // member variable private string name; private int index; // constructor private color (string name, int index) {This. name = Name; this. index = index;} // interface method @ overridepublic string getinfo () {return this. name;} // interface method @ overridepublic void print () {system. out. println (this. index + ":" + this. name );}}

Usage 6: organize enumeration Using Interfaces

 
Public interface food {Enum coffee implements food {black_coffee, decaf_coffee, latte, cappuccino} Enum dessert implements food {fruit, cake, Gelato }}

Usage 7: Use of enumeration Sets

Java. util. enumset and Java. util. enummap are two enumeration sets. Enumset ensures that the elements in the set are not repeated. The key in enummap is of the enum type, and the value can be of any type. The usage of these two sets is not described here. You can refer to the JDK documentation.

For the implementation details and principles of enumeration, refer:

Reference: Thinking in Java version 4

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.