Java enumeration Enum Usage Details

Source: Internet
Author: User

 

Java's Enum Enumeration type has finally appeared in j2se1.5. I thought it was just a chicken rib, but it was dispensable. After all, have you ever had a good life without it for so many years? Today, I read thinking in Java 4th edition, which contains a saying: "Sometimes it is precisely because of it that you can" elegantly and cleanly "solve the problem. Elegance and clarity are important. They formally differentiate successful and failed solutions. The solution to failure is because others have no legal principle. "Can I use the enum Enumeration type to make the previous clumsy code elegant and simple? However, I am also thinking about another question. Will the use of new technologies bring more burden to technicians?

"One danger of learning the new version of the language is the crazy use of the new syntax structure"

First, let's take a look at the simple application of enum. The following simple code already contains most of the functions provided by enum.

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

Java code

Import Java. util. enummap; <br/> Import Java. util. enumset; <br/> public class enumtest {<br/> // defines an Enum Enumeration type, including two instances on, off <br/> Public Enum state {<br/> On, off <br/> }; <br/> // test the function <br/> Public static void main (string [] ARGs) {<br/> // direct variable Enum <br/> for (State S: state. values () <br/> system. out. println (S. name (); <br/> // use the combination of switch and enum <br/> state switchstate = state. off; <br/> switch (switchstate) {<br/> case off: <br/> system. out. println ("off"); <br/> break; <br/> case on: <br/> system. out. println ("On"); <br/> break; <br/>}< br/> // use of enumset <br/> enumset <State> stateset = enumset. allof (state. class); <br/> for (State S: stateset) {<br/> system. out. println (s); <br/>}< br/> // use of enummap <br/> enummap <state, string> statemap = new enummap <state, string> (state. class); <br/> statemap. put (state. on, "is on"); <br/> statemap. put (state. off, "Is Off"); <br/> for (State S: state. values () {<br/> system. out. println (S. name () + ":" + statemap. get (s); <br/>}< br/> 

 

Package COM. aicent. test; <br/> Public Enum testenummathod {<br/> // Add different implementation methods for each Enum instance <br/> sample1 {<br/> string getinfo () {<br/> return "sample1"; <br/>}< br/>}, <br/> sample2 {<br/> string getinfo () {<br/> return "sample2"; <br/>}< br/>}; <br/> abstract string getinfo (); <br/> // test <br/> Public static void main (string ARGs []) {<br/> for (testenummathod method: values ()) {<br/> system. out. println (method. getinfo (); <br/>}< br/>}
The following content may be boring, but it is definitely worth a glimpse.
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 transform the enum instance to the parent class Enum, the value () is inaccessible. Solution: There is a getenumconstants () method in the class, so even if the enum interface does not have the values () method, we can still 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.