Collation: j2se5 enumeration example

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.