1. Define Enumeration
Public Enum grade {
A, B, C, D, F, incomplete
};
Ii. Traverse enumeration values
Public void listgradevalues (printstream out) throws ioexception {
For (grade G: grade. Values ()){
Out. println ("allowed value: '" + G + "'");
}
}
3. Use Enum in the switch
Public void testswitchstatement (printstream out) throws ioexception {
Stringbuffer outputtext = new stringbuffer (student1.getfullname ());
Switch (student1.getgrade ()){
Case:
Outputtext. append ("excelled with a grade of ");
Break;
Case B: // fall through to C
Case C:
Outputtext. append ("passed with a grade ")
. Append (student1.getgrade (). tostring ());
Break;
Case D: // fall through to F
Case F:
Outputtext. append ("failed with a grade ")
. Append (student1.getgrade (). tostring ());
Break;
Case incomplete:
Outputtext. append ("did not complete the class .");
Break;
Default:
Outputtext. append ("has a grade ")
. Append (student1.getgrade (). tostring ());
Break;
}
Out. println (outputtext. tostring ());
}
Iv. Use enumeration and set together
Public Enum antstatus {
Initializing,
Compiling,
Copying,
Jarring,
Zipping,
Done,
Error
}
Public void testenummap (printstream out) throws ioexception {
// Create a map with the key and a string message
Enummap <antstatus, string> antmessages =
New enummap <antstatus, string> (antstatus. Class );
// Initialize the map
Antmessages. Put (antstatus. Initializing, "Initializing ant ...");
Antmessages. Put (antstatus. Compiling, "Compiling Java classes ...");
Antmessages. Put (antstatus. Copying, "copying files ...");
Antmessages. Put (antstatus. jarring, "jarring up files ...");
Antmessages. Put (antstatus. zipping, "zipping up files ...");
Antmessages. Put (antstatus. Done, "Build complete .");
Antmessages. Put (antstatus. error, "error occurred .");
// Iterate and print messages
For (antstatus status: antstatus. Values ()){
Out. println ("For status" + status + ", message is:" +
Antmessages. Get (Status ));
}
}
Tiger treats enumeration as a class.AntStatusOfClassThe object proves that the object is not only available, but also being used. In the final analysis, tiger still regards enumeration as a special class type.