Java enum type enum use detail

Source: Internet
Author: User

  

The enum enum type of Java finally appeared in j2se1.5. Before thought it was just a chicken, dispensable. After all these years, without it, don't everyone have a good one? Today, "Thinking in Java" 4th edition, there is a sentence "sometimes precisely because of it, you can" elegant and clean "to solve the problem. Elegance and clarity are important, formally differentiating between successful solutions and failed solutions. And the solution to the failure is because other people can't talk to him. "Using enum enum types can make previously clumsy code elegant and simple?" However, I am also thinking about another problem, whether the use of new technology will bring more burdens to the technical staff?

"One of the dangers of learning a new language is the madness of using the syntax structure."

To learn about the simple application of enum, the following concise code already includes most of the functionality provided by the enum.

1.enum applications, including definition, traversal, SWITCH,ENUMSET,ENUMMAP, etc.

Java code

[C-sharp]View Plaincopyprint?
  1. Import Java.util.EnumMap;
  2. Import Java.util.EnumSet;
  3. Public class Enumtest {
  4. //define an enum enum type, including two instances On,off
  5. public enum State {
  6. On, OFF
  7. };
  8. //Test function
  9. public static void Main (string[] args) {
  10. //Direct variable enum
  11. For (state s:state.values ())
  12. System.  out.println (S.name ());
  13. //switch is used in conjunction with enum
  14. state switchState = State.off;
  15. switch (switchState) {
  16. Case OFF:
  17. System.  out.println ("OFF");
  18. Break ;
  19. Case on :
  20. System.  out.println ("on");
  21. Break ;
  22. }
  23. //Use of Enumset
  24. enumset<state> Stateset = Enumset.allof (state.   Class);
  25. For (state s:stateset) {
  26. System.  Out.println (s);
  27. }
  28. //Use of Enummap
  29. enummap<state,string> Statemap = new Enummap<state,string> (state.   Class);
  30. Statemap.put (State.on, "is on");
  31. Statemap.put (State.off, "is OFF");
  32. For (state s:state.values ()) {
  33. System. out.println (s.name () + ":" + statemap.   Get (s));
  34. }
  35. }
  36. }

[C-sharp]View Plaincopyprint?
  1. Package com.aicent.test;
  2. Public enum Testenummathod {
  3. //Add a different implementation method for each enum instance
  4. SAMPLE1 {
  5. String GetInfo () {
  6. return "SAMPLE1";
  7. }
  8. },
  9. SAMPLE2 {
  10. String GetInfo () {
  11. return "SAMPLE2";
  12. }
  13. };
  14. abstract String getInfo ();
  15. //Test
  16. public static void Main (String args[]) {
  17. For (Testenummathod method:values ()) {
  18. System.  out.println (Method.getinfo ());
  19. }
  20. }
  21. }

The following may be a bit boring, but definitely worth a glimpse
1. Code:
public class State {
public static final int on = 1;
public static final Int off= 0;
}

What's wrong, everyone has been so used for a long time, no problem ah.
First, it is not type-safe. You have to make sure it's int.
Second, you have to make sure it's 0 and 1.
Finally, many times when you print out, you only see 1 and 0,

But the person who doesn't see the code doesn't know what you're trying to do, discard all your old public static final constants.

2. You can create an enum class that is considered an ordinary class. Except that it cannot inherit other classes. (Java is a single inheritance, it has inherited an enum),
You can add other methods that override the method of its own

The 3.switch () parameter can use enum

The 4.values () method is the static method that the compiler inserts into the enum definition, so when you convert an enum instance up to a parent enum, values () is inaccessible. Workaround: There is a getenumconstants () method in class, so even if the enum interface does not have the values () method, we can still get all the enum instances through the class object

5. You cannot inherit subclasses from an enum, and if you need to extend the elements in an enum, within an interface, create an enumeration that implements that interface to group the elements. The enumeration elements are grouped to be reached.

6. Use Enumset instead of logo. An enum requires that its members are unique, but the add element cannot be removed from the enum.

The key for 7.EnumMap is Enum,value is any other object.

8.enum allows programmers to write methods for EUNM instances. Therefore, each enum instance can be given a different behavior.

9. Use the Enum's responsibility chain (Chain of Responsibility). This is related to the design pattern of the responsibility chain model. Solve a problem in a number of different ways. Then link them together. When a request arrives, it traverses the chain until a solution in the chain can handle the request.

10. State machine using enum

Java enum type enum use detail

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.