1. About Java Enum:
People who have studied languages such as C + + should inkling the Enum type. An Enum is typically used to represent a set of constants of the same type. such as sex, date, month, color and so on. The benefits of constants for these properties are obvious, not only for a single example, but also for equals with "= =" when compared. It's a good habit. There is no Enum this type before JDK1.5, which is typically replaced with an interface constant. With the Javaenum, you can express this constant more closely. 2. How to use Java Enum
Simple usage: javaenum simple usage is typically used to represent a set of common constants that can be used to represent a constant value of a class of the same type. Such as:
Gender:
Public enum Sexenum {
male, female;
}
Color:
Public enum Color {
RED, Blue,green,black;
}
The values in an enumeration object must be unique.
The constant can be referenced directly by the Enum type name, such as sexenum.male,color.red.
Note: above excerpted from Csdn:abing37
Complex usage: Java provides some built-in methods for enumeration types, and colleagues can also have their own methods for enumerating constants. It is convenient to traverse the enumeration object.
Here are a few sample codes: Code 1:enummethod.java The following are common ways to enumerate types:
/** * Author Owenyang * Data 2012-11-2/import static java.lang.System.out; public class Enummethod {enum sexenum{male, Famle, shemale}//compareto () public static void Comp Are (Sexenum sexe) {for (int i = 0; I <sexenum.values (). length, i++) {out.println ("The result of" +sexe+ "compare
To "+sexenum.values () [i]+": "+ Sexe.compareto (sexenum.values () [i])); } public static void Main (string[] args) {//TODO auto-generated a stub for (Sexenum Sex:SexEnum.valu
ES ()) {out.println ("Enum member Variable:" +sex);
//use another way to show out.println ("------------use another way to show------------");
for (int i =0;i<sexenum.values (). length;i++) {out.println ("Enum member Variable:" +sexenum.values () [i]);
}//compareto () and valueof () compare (sexenum.valueof ("shemale")); Ordinal () to get the index of member variable for (Sexenum sex:SexEnum.values ()) {out.println ("INdex of "+sex+" "+sex.ordinal ()); }
}
}
Code 2:enumconstructor.javaThe following is the constructor method in the enumeration type using the
public class Enumconstructor {//constructor usage enum sexenum{male ("I Am a Mans"), Famle ("I Am a Woman"),
Shemale ("I am a Shemale");
Private String description;
Private Sexenum (String str) {this.description=str;
Public String GetDescription () {return this.description; }//we can also define the GetDescription in interface interface public String getdesc{()
;
The enum Extraenum implements getdesc{male{public String getdescription () {return ' I am a Super man ';
}, famle{public String getdescription () {return "I am a Super woman"; }} public static void Main (string[] args) {//TODO auto-generated a stub for (Sexenum Sex:sexenum
. values ()) {System.out.println ("Info:" +sex.getdescription ()); }//test interface for (int i =0;i<extraenum.values (). length;i++) {System.out.println (extraenum.values () [I ]+ "Invoke GetDescription: "+extraenum.values () [I].getdescription ()]; }
}
}
Code 3:enumerationtest.java
The following is a simple example of an enumeration interface:
Import java.util.Enumeration;
Import Java.util.Vector;
public class Enumerationtest {public
static void Main (string[] args) {
//TODO auto-generated method stub
Vec tor<integer> vector = new vector<integer> ();
for (int i =0;i<5;i++) {
vector.add (i);
System.out.println ("Add elements to vector:" +i);
}
Converts a vector to an enumerated interface type
enumeration<integer> e = vector.elements ();
while (E.hasmoreelements ()) {
System.out.println ("element:" +e.nextelement ());}}}
Above, most of which are excerpted from cattle, the code has been measured.