Java learns java from scratch (enumeration definition and simple use), and Java starts from scratch

Source: Internet
Author: User

Java learns java from scratch (enumeration definition and simple use), and Java starts from scratch
I. Enumeration

Enumeration refers to a type composed of a set of fixed constants, indicating a specific data set, but all possible values are known when this data set is defined.

We recommend that you use uppercase letters for the names of enumerated constants.

Enumerated constants are static enumerated fields, which are separated by commas.

Use the enum keyword to define an enumeration.

Package com. pb. demo1;/** days of one Week for Enumeration type */public enum Week {// MON, TUE, WED, THU, FRI, SAT, SUN} from Monday to Sunday}
Package com. pb. demo1; public class WeekDemoTest {/** indicates the day from Monday to Sunday for the input */public void doWhat (Week day) {switch (day) {case MON: System. out. println ("Monday, first day of work"); break; case TUE: System. out. println ("Tuesday, the second day"); break; case WED: System. out. println ("Wednesday, two other weekends"); break; case THU: System. out. println ("Thursday, another day"); break; case FRI: System. out. println ("Friday, today's weekend"); break; case SAT: System. out. printl N ("Saturday, resting, watching movies"); break; case SUN: System. out. println ("Sunday, rest, play"); break; default: System. out. println ("seven days a week on Earth! ") ;}} Public static void main (String [] args) {WeekDemoTest wdt = new WeekDemoTest (); // declare an enumeration type Week day = Week. THU; wdt. doWhat (day); // directly use wdt without declaring the enumeration type. doWhat (Week. TUE); System. out. println ("=========== traverse enumeration ============"); // traverse enumeration for (Week w: Week. values () {System. out. println (w );}}}

 

Related Article

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.