Java starts from zero 25 (enumeration definition and simple use)

Source: Internet
Author: User

First, enumerate

Enumerations are types that consist of a fixed set of constants that represent a specific set of data, except that all possible values are known when the data collection is defined.

The name of the enumeration constant is proposed in uppercase.

Enumeration constants are static fields of enumerations, and enumeration constants are separated by commas.

Use a keyword enum to define an enumeration

 Package Com.pb.demo1; /*  */publicenum  Week    {// Monday to Sunday     MON, Tue,wed,thu,fri,sat,sun}
 PackageCom.pb.demo1; Public classWeekdemotest {/** from Monday to Sunday what day do you fix?*/     Public voiddowhat (Week day) {Switch(day) { CaseMON:System.out.println ("Monday, first day of work");  Break;  CaseTUE:System.out.println ("Tuesday, the second day of the bitter force.");  Break;  CaseWED:System.out.println ("Wednesday, there are two genius weekends.");  Break;  CaseTHU:System.out.println ("Thursday, come on, there's a day.");  Break;  CaseFRI:System.out.println ("Friday, after today's weekend.");  Break;  CaseSAT:System.out.println ("Saturday, rest, see the movie");  Break;  CaseSUN:System.out.println ("Sunday, rest and play");  Break; default: System.out.println ("7 days a week on earth!" "); }    }             Public Static voidMain (string[] args) {weekdemotest WDT=Newweekdemotest (); //declaring an enumeration typeWeek Day =Week.thu;        Wdt.dowhat (day); //Direct use without declaring enum type directlyWdt.dowhat (Week.tue); System.out.println ("========= Traversal enumeration ==========="); //Traversal Enumeration         for(Week w:week.values ()) {System.out.println (w); }    }}

Java starts from zero 25 (enumeration definition and simple use)

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.