Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/
Java. Lang. Enum> Use enum.
Class:
Public class enumdemo {
Enum edge {
Top, bottom, left, right // defines an instance of the enum type. An Enumeration type has no instances except those defined by enumeration constants.
};
Public static void main (string Arg []) {
Edge edge1 = edge. Left;
Edge edge2 = edge. Left;
// Since only one instance of each Enumeration type can exist in one program, we can use = to determine whether it is the same.
System. Out. println ("can we use = to judge equals:" + (edge1 = edge2 ));
Int I = edge1.ordinal ();
System. Out. println ("the value of I:" + I );
System. Out. println (edge1 );
String STR = "";
Switch (edge1 ){
/* Case edge. Top: This is wrong */
Case top: Str = "the top edge"; break;
Case bottom: Str = "the bottom edge"; break;
Case left: Str = "the left edge"; break;
Case right: Str = "the right edge"; break;
}
System. Out. println (STR );
}
This solves the disadvantages that the switch cannot perform on strings.Note: The Enumeration type is also a class, and its members are actually static final Enumeration type instances.
To better understand enumeration types, weSimulate this enumeration class with a common class:
Public abstract class weekday {
Private int workhours;
Private Boolean work;
Private weekday (INT workhours, Boolean work ){
This. workhours = workhours;
This. Work = work;
}
Protected abstract weekday getnextday ();
Public static final weekday Monday = new weekday (8, true ){
Public weekday getnextday (){
Return Tuesday;
}
@ Override
Public String tostring (){
Return "Monday ";
}
};
Public static final weekday Tuesday = new weekday (8, true ){
Public weekday getnextday (){
Return Wednesday;
}
@ Override
Public String tostring (){
Return "Tuesday ";
}
};
Public static final weekday Wednesday = new weekday (8, true ){
Public weekday getnextday (){
Return Thursday;
}
@ Override
Public String tostring (){
Return "Wednesday ";
}
};
Public static final weekday Thursday = new weekday (8, true ){
Public weekday getnextday (){
Return Friday;
}
@ Override
Public String tostring (){
Return "Thursday ";
}
};
Public static final weekday Friday = new weekday (8, true ){
Public weekday getnextday (){
Return Saturday;
}
@ Override
Public String tostring (){
Return "Friday ";
}
};
Public static final weekday Saturday = new weekday (4, true ){
Public weekday getnextday (){
Return Sunday;
}
@ Override
Public String tostring (){
Return "Saturday ";
}
};
Public static final weekday Sunday = new weekday (0, false ){
Public weekday getnextday (){
Return Monday;
}
@ Override
Public String tostring (){
Return "Sunday ";
}
};
Private Static void showday (weekday d ){
If (D. isweekday ()){
System. Out. println (D + "is a weekday and has" +
D. gethours () + "working hours .");
} Else {
System. Out. println (D + "is not a weekday and has" +
D. gethours () + "working hours .");
}
}
Private int gethours (){
// Todo auto-generated method stub
Return workhours;
}
Private Boolean isweekday (){
Return work;
}
Public static void main (string [] ARGs ){
Weekday day = weekday. Friday;
Showday (day );
Day = weekday. Friday. getnextday ();
Showday (day );
Day = day. getnextday ();
Showday (day );
}
}
Therefore, there are the following more complex applications:
Public Enum day {
Monday (8, true) {// first, the class constructor is implemented using the Anonymous class inherited by the subclass, and the abstract method of the parent class is completed in the subclass.
Public day getnextday (){
Return Tuesday;
}
},
Tuesday (8, true ){
Public day getnextday (){
Return Wednesday;
}
},
Wednesday (8, true ){
Public day getnextday (){
Return Thursday;
}
},
Thursday (8, true ){
Public day getnextday (){
Return Friday;
}
},
Friday (8, true ){
Public day getnextday (){
Return Saturday;
}
},
Saturday (4, false ){
Public day getnextday (){
Return Sunday;
}
},
Sunday (0, false ){
Public day getnextday (){
Return Monday;
}
};
Private int hours;
Private Boolean weekday;
Day (INT workhours, Boolean weekdayflag ){
Hours = workhours;
Weekday = weekdayflag;
}
Private abstract day getnextday ();
Public int gethours (){
Return (hours );
}
Public Boolean isweekday (){
Return (weekday );
}
Private Static void showday (day d ){
If (D. isweekday ()){
System. Out. println (D + "is a weekday and has" +
D. gethours () + "working hours .");
} Else {
System. Out. println (D + "is not a weekday and has" +
D. gethours () + "working hours .");
}
}
Public static void main (string Arg []) {
Day;
Day = day. Tuesday;
Showday (day );
Day = day. Saturday;
Showday (day );
Day = day. Thursday. getnextday ();
Showday (day );
}
}
Note:
1. Note that the enumeration type cannot be abstract or final.
2. the abstract method is defined in the enumeration class, but the enumeration class does not have an instance to implement this abstract method. In this case, an error is reported.
3. An Enumeration type is an implicit final unless it contains at least one enumeration constant with a class body.
4.Nesting(Enumeration defined in a class) The Enumeration type is implicitly static. If it is displayed, an error occurs.
5. enumset contains many static methods used to specify different set creation and read methods.
Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/