Topic: Java enumeration 7 common usage

Source: Internet
Author: User

Dk1.5 introduces a new type-enumeration. In Java, although it is regarded as a "small" function, it brings great convenience to my development.

I think the most commonly used is: usage 1, usage 2 usage 1: Constant

Before jdk1.5, we define constants as publicstaticfianl ..... 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, and uses enumeration to make our code more 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 must add a semicolon at the end of the enum instance sequence. In addition, Java requires that Enum be defined first.
Instance.

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. Because Java does not support multi-inheritance, 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: thinkinginjava 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.