[Java] Java Enum

Source: Internet
Author: User

1. Application of Enum, including definition, traversal, switch, enumset, enummap, etc.

Import Java. util. enummap; import Java. util. enumset; public class enumtest {// defines an Enum Enumeration type, including two instances on, off public Enum state {On, off }; // test the public static void main (string [] ARGs) {// direct variable Enum for (State S: state. values () system. out. println (S. name (); // The combination of switch and enum uses state switchstate = state. off; Switch (switchstate) {case off: system. out. println ("off"); break; Case on: system. out. println ("On"); break;} // use enumset for enumset <State> stateset = enumset. allof (state. class); For (State S: stateset) {system. out. println (s);} // use enummap for enummap <state, string> statemap = new enummap <state, string> (state. class); statemap. put (state. on, "is on"); statemap. put (state. off, "Is Off"); For (State S: state. values () {system. out. println (S. name () + ":" + statemap. get (s ));}}}
Package COM. aicent. test; Public Enum testenummathod {// Add different implementation methods sample1 {string getinfo () {return "sample1" ;}, sample2 {string getinfo () for each Enum instance () {return "sample2" ;}}; abstract string getinfo (); // test public static void main (string ARGs []) {for (testenummathod method: values ()) {system. out. println (method. getinfo ());}}}
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 ;}}
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 @ override Public String getinfo () {return this. name;} // interface method @ override public void print () {system. out. println (this. index + ":" + this. name );}}
Organize enumeration Using Interfaces
    public   interface  Food {          enum  Coffee  implements  Food{              BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO          }          enum  Dessert  implements  Food{              FRUIT, CAKE, GELATO          }      }  
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.

 

========================================================== ================

1. Code:

public class State {    public static final int ON = 1;    public static final Int OFF= 0;}

What's wrong? It's been a long time for everyone to do this. It's okay.
First, it is not type-safe. You must make sure it is an int
Second, make sure that the range is 0 and 1.
Finally, most of the time you print out, you only see 1 and 0,

But the person who does not see the Code does not know your attempt. Abandon all your old public static final constants.

2. You can create an Enum class and think of it as a common class. Except that it cannot inherit other classes. (Java is a single inheritance, and it has inherited Enum ),
You can add other methods to overwrite them.

3. You can use Enum for the switch () parameter.

4. The values () method is the static method inserted by the compiler into the enum definition. Therefore, when you convert the enum instance to the parent class Enum, the value ()
It will be inaccessible. Solution: There is a getenumconstants () method in the class, so even if the enum interface does not have the values () method, we can
To get all Enum instances through the Class Object

5. The subclass cannot be inherited from enum. If you need to extend the elements in Enum, you can create an enumeration for this interface within an interface to group the elements. To group the enumerated elements.

6. Use enumset to replace the flag. Enum requires that its members be unique, but Enum cannot delete the add element.

7. The key of enummap is Enum, and value is any other object.

8. Enum allows programmers to write methods for eunm instances. Therefore, you can assign different behaviors to each Enum instance.

9. Use the responsibility chain (chain of responsibility) of enum. This is the responsibility chain mode related to the design model. Solve a problem in multiple ways. Then link them together. When a request arrives, traverse the chain until a solution in the chain can process the request.

10. State Machine with Enum

11. Use Enum for multi-channel distribution

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.