Common uses of Java enumeration 7

Source: Internet
Author: User

Java Enumeration ENMU

JDK1.5 introduces a new type--enumeration. In Java, although it is a "small" function, but to my development has brought "big" convenience.

Usage One: Constants

before JDK1.5, we defined constants: public static fianl ..... Now, with enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants.

Java code
    1. Public enum Color {
    2. RED, GREEN, BLANK, YELLOW
    3. }
public enum Color {  RED, GREEN, BLANK, YELLOW}
Usage Two: Switch

The switch statement before JDK1.6 only supports int,char,enum types, and using enumerations can make our code more readable.

Java code
  1. Enum Signal {
  2. GREEN, YELLOW, RED
  3. }
  4. Public class TrafficLight {
  5. Signal color = signal.red;
  6. public Void Change () {
  7. switch (color) {
  8. Case RED:
  9. color = Signal.green;
  10. Break ;
  11. Case YELLOW:
  12. color = signal.red;
  13. Break ;
  14. Case GREEN:
  15. color = Signal.yellow;
  16. Break ;
  17. }
  18. }
  19. }
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 Three: Add a new method to the enumeration

If you intend to customize your own method, you must add a semicolon at the end of the enum instance sequence. And Java requires that an enum instance be defined first .

Java code
  1. Public enum Color {
  2. Red ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);
  3. //Member variables
  4. private String name;
  5. private int index;
  6. //Construction method
  7. Private Color (String name, int index) {
  8. this.name = name;
  9. This.index = index;
  10. }
  11. //Common method
  12. public static String getName (int index) {
  13. For (Color c:color.values ()) {
  14. if (c.getindex () = = index) {
  15. return c.name;
  16. }
  17. }
  18. return null;
  19. }
  20. //Get Set Method
  21. Public String GetName () {
  22. return name;
  23. }
  24. public void SetName (String name) {
  25. this.name = name;
  26. }
  27. public int GetIndex () {
  28. return index;
  29. }
  30. public void Setindex (int index) {
  31. This.index = index;
  32. }
  33. }
The public enum Color {red ("red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);//member variable private String name;private int ind ex;//Construction Method Private Color (String name, int index) {this.name = Name;this.index = index;} Normal 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 Four: Methods for overriding enumerations

An example of the ToString () method overlay is given below.

Java code
  1. Public enum Color {
  2. Red ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);
  3. //Member variables
  4. private String name;
  5. private int index;
  6. //Construction method
  7. Private Color (String name, int index) {
  8. this.name = name;
  9. This.index = index;
  10. }
  11. //Coverage Method
  12. @Override
  13. Public String toString () {
  14. return this.index+"_" +this.name;
  15. }
  16. }
The public enum Color {red ("red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);//member variable private String name;private int ind ex;//Construction Method Private Color (String name, int index) {this.name = Name;this.index = index;} Override method @overridepublic String toString () {return this.index+ "_" +this.name;}}
Usage Five: Implement Interface

All enumerations are inherited from the Java.lang.Enum class. Because Java does not support multiple inheritance, enumeration objects can no longer inherit from other classes.

Java code
  1. Public interface Behaviour {
  2. void print ();
  3. String GetInfo ();
  4. }
  5. Public enum Color implements behaviour{
  6. Red ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);
  7. //Member variables
  8. private String name;
  9. private int index;
  10. //Construction method
  11. Private Color (String name, int index) {
  12. this.name = name;
  13. This.index = index;
  14. }
  15. Interface method
  16. @Override
  17. Public String GetInfo () {
  18. return this.name;
  19. }
  20. //interface method
  21. @Override
  22. public void print () {
  23. System.out.println (this.index+":" +this.name);
  24. }
  25. }
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;//Construction method 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 VI: Organizing enumerations using interfaces

Java code
    1. Public interface Food {
    2. enum Coffee implements food{
    3. Black_coffee,decaf_coffee,latte,cappuccino
    4. }
    5. enum Dessert implements food{
    6. FRUIT, CAKE, GELATO
    7. }
    8. }
Public interface Food {enum Coffee implements Food{black_coffee,decaf_coffee,latte,cappuccino}enum dessert implements Food{fruit, CAKE, GELATO}}
Usage Seven: About the use of enumeration collections

Java.util.EnumSet and Java.util.EnumMap are two enumeration collections. Enumset guarantees that the elements in the collection are not duplicated; The key in Enummap is the enum type, and value can be any type. The use of this two collection is not mentioned here, you can refer to the JDK documentation.

For the implementation details and principles of enumerations, please refer to:

Reference: "Thinkinginjava" fourth edition

Common uses of Java enumeration 7

Related Article

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.