DK1.5 introduces a new type--enumeration. In Java, although it is a "small" function, but to my development has brought "big" convenience.
Usage One: Constants
Before JDK1.5, we define constants that are: publicstaticfianl ..... Now, with enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants.
Java code
public enum Color {
RED, GREEN, BLANK, YELLOW
}
Usage Two: Switch
The switch statement before JDK1.6 only supports int,char,enum types, and using enumerations can 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 three: Adding a new method to an enumeration
If you intend to customize your own method, you must add a semicolon at the end of the enum instance sequence. And Java requires that an enum instance be defined first.
Java code
Public enum Color {
Red ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);
//Member variables
Private String name;
Private int index;
//Construction method
Private Color (String name, int index) {
THIS.name = name;
This.index = index;
}
//Normal 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 Four: Overriding enumeration methods
Gives an example of the ToString () method override.
Java code
Public enum Color {
Red ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);
//Member variables
Private String name;
Private int index;
//Construction method
Private Color (String name, int index) {
THIS.name = name;
This.index = index;
}
//override method
@Override
Public String toString () {
Return this.index+ "_" +this.name;
}
}
-
Usage Five: Implement interface
All enumerations inherit from the Java.lang.Enum class. Because Java does not support multiple inheritance, enumeration objects can no longer inherit from 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 variables
Private String name;
Private int index;
//Construction method
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 VI: Organizing enumerations using interfaces
Java code
Public interface Food {
Enum Coffee implements food{
Black_coffee,decaf_coffee,latte,cappuccino
}
Enum Dessert implements food{
FRUIT, CAKE, GELATO
}
}
- 7
Usage Seven: About the use of enumeration collections
Java.util.EnumSet and Java.util.EnumMap are two enumeration collections. Enumset guarantees that the elements in the collection are not duplicated; The key in Enummap is the enum type, and value can be any type. The use of this two collection is not mentioned here, you can refer to the JDK documentation.