Java enumeration 7 common usage, java enumeration 7 usage

Source: Internet
Author: User

Java enumeration 7 common usage, java enumeration 7 usage

DK1.5 introduces a new type-enumeration. In Java, although it is regarded as a "small" function, it brings great convenience to my development.

Usage 1: Constant

Before JDK1.5, we define constants as public static fianl ..... 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 Color implements 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


What is enumeration in Java? What is his usage?

What is enumeration in Java? What is his usage?

Enumeration is to extract the object elements in the set one by one! For example, if you have bought a chocolate with a strawberry flavor, a vanilla flavor, and an apple flavor, you must use your hands to take them out one by one and only one at a time. the operator is the enumerator. Your action is the enumeration process.

I am not sure about the specific usage. I suggest you look at the JDK1.5 API and reference others' examples.
After reading this, you can understand several enumeration options (two types are available: iterator and enumerator) and how to determine whether the object elements have been obtained, etc. I am not very fond of speaking the syntax, so I am sorry, I will not lose code for you ~

What is enumeration in Java? What is his usage?

What is enumeration in Java? What is his usage?

Enumeration is to extract the object elements in the set one by one! For example, if you have bought a chocolate with a strawberry flavor, a vanilla flavor, and an apple flavor, you must use your hands to take them out one by one and only one at a time. the operator is the enumerator. Your action is the enumeration process.

I am not sure about the specific usage. I suggest you look at the JDK1.5 API and reference others' examples.
After reading this, you can understand several enumeration options (two types are available: iterator and enumerator) and how to determine whether the object elements have been obtained, etc. I am not very fond of speaking the syntax, so I am sorry, I will not lose code for you ~

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.