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
- Public enum Color {
- RED, GREEN, BLANK, YELLOW
- }
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
- Enum Signal {
- GREEN, YELLOW, RED
- }
- Public class TrafficLight {
- Signal color = Signal. RED;
- Public void change (){
- Switch (color ){
- Case RED:
- Color = Signal. GREEN;
- Break;
- Case YELLOW:
- Color = Signal. RED;
- Break;
- Case GREEN:
- Color = Signal. YELLOW;
- Break;
- }
- }
- }
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
- 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;
- }
- }
Usage 4: override Enumeration Method
The following is an example of overwriting the toString () method.
Java code
- 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;
- }
- // Override method
- @ Override
- Public String toString (){
- Return this. index + "_" + this. name;
- }
- }
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
- 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 );
- }
- }
Usage 6: organize enumeration Using Interfaces
Java code
- Public interface Food {
- Enum Coffee implements Food {
- BLACK_COFFEE, DECAF_COFFEE, LATTE, CAPPUCCINO
- }
- Enum Dessert implements Food {
- FRUIT, CAKE, GELATO
- }
- }
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 ~