Reprint: Java Enumeration (enum) detailed 7 common usage
http://blog.csdn.net/qq_27093465/article/details/52180865
JDK1.5 introduces a new type--enumeration. In Java it is a "small" function, but to my development has brought "big" convenience.
usage One: Constants
usage two: switch
usage Three: Add a new method to the enumeration
Usage Six: Organize enumerations using interfaces
Usage Five: Implement Interface
Usage Six: Organize enumerations using interfaces
usage Seven: About the use of enumeration collections usage One: Constants
Before JDK1.5, we defined constants as: public static fianl ... Now that you have enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants.
public enum Color {
RED, GREEN, BLANK, Yellow
usage two: switch
The switch statement before JDK1.6 only supports the Int,char,enum type, and using enumerations makes our code more readable.
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: Add a new method to the enumeration
If you intend to customize your own method, you must add a semicolon at the end of the sequence of enum instances. and Java requirements must first define an instance of an enum.
The public enum Color {red
("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);
Member variable
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 () = = Inde x) {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: Methods of overriding enumerations
An example of a ToString () method overlay is given below.
The public enum Color {red
("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);
Member variable
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 other classes.
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;
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 Six: Organize enumerations using interfaces
Public interface Food {
enum Coffee implements food{
Black_coffee,decaf_coffee,latte,cappuccino
}
enum Dessert implements food{
FRUIT, CAKE, GELATO
}
}
usage Seven: About the use of enumeration collections
Java.util.EnumSet and Java.util.EnumMap are two enumerated collections.
Enumset guarantees that the elements in the collection are not duplicates;
The key in Enummap is the enum type, and value can be any type.
The use of this two collection is not here to repeat, you can refer to the JDK documentation.
for the implementation details and principles of enumerations, refer to:
Reference: "Thinkinginjava" fourth edition
http://softbeta.iteye.com/blog/1185573 Note: Enumeration type objects are compared to values that can be used = =, directly to compare values, whether equality, not the need to use the Equals method yo.