All enumeration types in Java are subclasses of java. lang. Enum.
1. Define enumeration types
A simple enumeration type is defined as follows:
public enum Week {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;}
2. Common enumeration methods
You can use the values () Static Method of the enumerated type to return all the enumerated values in the enumerated type, use the name () method of the enumerated constant to return the name of the enumerated constant, and use ordinal () of the enumerated constant () returns the ordinal number of an enumerated constant (The position of a constant in the enumeration declaration, starting from 0 ). The sample code is as follows:
Public static void main (String [] args) {// retrieve all enumerated values of the enumeration type Week [] days = Week. values (); for (Week day: days) {// return the enumerated constant name String name = day. name (); // returns the ordinal number of the enumerated constant (position of the constant in the enumeration declaration, starting from 0) int ordinal = day. ordinal (); String toString = day. toString (); Class declaringClass = day. getDeclaringClass (); Class superClass = declaringClass. getSuperclass (); System. out. println ("Name:" + name + "\ n" + "Ordinal:" + ordinal + "\ n" + "ToString: "+ toString +" \ n "+" DeclaringClass: "+ declaringClass +" \ n "+" SuperClass: "+ superClass +" \ n ");}}
The output result is as follows:
Name: MONDAYOrdinal: 0ToString: MONDAYDeclaringClass: class myEnum.WeekSuperClass: class java.lang.EnumName: TUESDAYOrdinal: 1ToString: TUESDAYDeclaringClass: class myEnum.WeekSuperClass: class java.lang.EnumName: WEDNESDAYOrdinal: 2ToString: WEDNESDAYDeclaringClass: class myEnum.WeekSuperClass: class java.lang.EnumName: THURSDAYOrdinal: 3ToString: THURSDAYDeclaringClass: class myEnum.WeekSuperClass: class java.lang.EnumName: FRIDAYOrdinal: 4ToString: FRIDAYDeclaringClass: class myEnum.WeekSuperClass: class java.lang.EnumName: SATURDAYOrdinal: 5ToString: SATURDAYDeclaringClass: class myEnum.WeekSuperClass: class java.lang.EnumName: SUNDAYOrdinal: 6ToString: SUNDAYDeclaringClass: class myEnum.WeekSuperClass: class java.lang.Enum
3. Compare enumerated Constants
To determine whether the values of two enumerated constants are equal, you can directly use "=" instead of the equals () method. You can use the compareTo () method to compare the values of two enumerated constants.
Public static void main (String [] args) {Week mon = Week. MONDAY; Week tues = Week. TUESDAY; Week wed = Week. WEDNESDAY; // compare enumerated constants, which are actually the size of their ordinal numbers. out. println (mon. compareTo (tues); System. out. println (tues. compareTo (wed); System. out. println (wed. compareTo (mon ));}
The output result is as follows:
-1-12
In fact, comparing the sizes of two enumerated constants is to compare the ordinal sizes of their ordinal numbers. In the Enum source code of JDK1.6, The compareTo () method is as follows:
public final int compareTo(E o) {Enum other = (Enum)o;Enum self = this;if (self.getClass() != other.getClass() && // optimization self.getDeclaringClass() != other.getDeclaringClass()) throw new ClassCastException();return self.ordinal - other.ordinal;}
4. Create an enumerated constant
There are three main methods to create an enumerated constant: Creating an enumerated Constant Using the enumerated type, creating an enumerated Constant Using the Enum valueOf () static method, and using the enumerated type valueOf () create an enumerated Constant Using static methods.
// Directly create the enumerated constant Week mon = Week. MONDAY; // use the valueOf () Static Method of Enum to create the enumerated constant Week tues = Enum. valueOf (Week. class, "TUESDAY"); // use the valueOf () Static Method of Enumeration type to create the enumerated constant Week wed = Week. valueOf ("WEDNESDAY ");
5. Define advanced enumeration types
You can add constructors, methods, and fields to the enumeration type as needed, and enable the enumeration type to inherit interfaces.
Define the IWeek interface as follows:
public interface IWeek {public void println();}
Defines advanced enumeration types, including AdvancedWeek, constructor, method, and field, and implements the IWeek interface.
Public enum AdvancedWeek implements IWeek {MONDAY ("MONDAY", "Mon. "), TUESDAY (" TUESDAY "," Tues. "), WEDNESDAY (" WEDNESDAY "," Wed. "), THURSDAY (" Thurs "," Thurs. "), FRIDAY (" FRIDAY "," Fri. "), SATURDAY (" SATURDAY "," Sat. "), SUNDAY (" SUNDAY "," Sun. "); private String nameCn; private String abbreviation; // The constructor can only be privateprivate AdvancedWeek () {} private AdvancedWeek (String nameCn, String abbreviation) {this. setNameCn (nameCn); this. setAbbreviation (abbreviation);} public String getNameCn () {return nameCn;} public void setNameCn (String nameCn) {this. nameCn = nameCn;} public String getAbbreviation () {return abbreviation;} public void setAbbreviation (String abbreviation) {this. abbreviation = abbreviation;} public void println () {System. out. println (this. ordinal () + "-" + this. name () + "-" + this. getAbbreviation () + "-" + this. getNameCn ());}}
Note that the enumeration Type constructor can only be private.
Test AdvancedWeek as follows:
public static void main(String[] args) {AdvancedWeek[] days = AdvancedWeek.values();for(AdvancedWeek day : days) {day.println();}}
Output result:
0-MONDAY-Mon. -Monday 1-TUESDAY-Tues. -Tuesday 2-WEDNESDAY-Wed. -weds3-thursday-Thurs. -Thursday 4-FRIDAY-Fri. -Friday 5-SATURDAY-Sat. -Saturday 6-SUNDAY-Sun. -Sunday