Seven common Java enumeration 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. Usage 1: Before JDK1.5, we define all constants as 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 statements before switch JDK1.6 only support int, char, and enum types, and use 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 the enum instance be defined first. 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: overwrite the enumeration method. An example of overwrite the toString () method is given below. 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 @ Override public String toString () {return this. index + "_" + this. name ;}} usage 5: All enumeration operations are inherited from 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 @ Override public String getInfo () {return this. name;} // interface method @ Override public void print () {System. out. println (this. index + ":" + this. name) ;}} usage 6: Use the interface to organize the enumeration of public interface Food {enum Coffee implements Food {BLACK_COFFEE, DECAF_COFFEE, LATTE, CAPPUCCINO} enum Dessert implements Food {FRUIT, CAKE, GELATO} usage 7: java for enumeration sets. util. enumSet and java. util. enumMap is an enumeration set. 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.

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.