7 common Java enumeration usage

Source: Internet
Author: User
Usage 1: Constant

Before jdk1.5, we define constants as publicstaticfianl ..... Now, with enumeration, You can group related constants into one Enumeration type, and enumeration provides more methods than constants.

Java code
  1. Public Enum color {
  2. Red, green, blank, yellow
  3. }
Usage 2: Switch

The switch statement before jdk1.6 only supports int, Char, and enum types, and uses enumeration to 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. }

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.

Java code
  1. Public Enum color {
  2. Red ("red", 1), Green ("green", 2 ),
    Blank ("white", 3), yello ("yellow", 4 );
  3. // Member variable
  4. Private string name;
  5. Private int index;
  6. // Constructor
  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. }

Usage 4: override Enumeration Method

The following is an example of overwriting the tostring () method.

Java code
  1. Public Enum color {
  2. Red ("red", 1), Green ("green", 2 ),
    Blank ("white", 3), yello ("yellow", 4 );
  3. // Member variable
  4. Private string name;
  5. Private int index;
  6. // Constructor
  7. Private color (string name,
    Int index ){
  8. This. Name = Name;
  9. This. Index = index;
  10. }
  11. // Override method
  12. @ Override
  13. Public String tostring (){
  14. Return this. index + "_" + this. Name;
  15. }
  16. }
Usage 5: 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.

Java code
  1. Public Interface Behaviour {
  2. Void print ();
  3. String getinfo ();
  4. }
  5. Public Enum colorimplements behaviour {
  6. Red ("red", 1), Green ("green", 2 ),
    Blank ("white", 3), yello ("yellow", 4 );
  7. // Member variable
  8. Private string name;
  9. Private int index;
  10. // Constructor
  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. }
Usage 6: organize enumeration 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. }
Usage 7: 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.

For the implementation details and principles of enumeration, refer:

Reference: thinkinginjava version 4

Http://softbeta.iteye.com/blog/1185573

 

Java enumeration can be used in combination with jstl labels and hibernate technologies. The effect of Java enumeration is greatly enhanced, and the code is clear and maintainability is high.

Reprint please indicate the source http://blog.csdn.net/shimiso

Technical Exchange Group: 173711587

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.