Application of Java Enumeration

Source: Internet
Author: User

Use enumerations to implement the example described in the previous article: and add functionality to output the content of today's execution;

Import Java.util.Calendar;
Import Java.util.Date;

/**
* Enumeration of weeks
*/
Public Enum Day {

Work (1, 2, 3, 4, 5) {
@Override
public void dothing () {
System.out.println ("Work ... ");
}
},//working days
STA (6) {
@Override
public void dothing () {
System.out.println ("Fitness ... ");
}
},//Saturday
SUN (0) {
@Override
public void dothing () {
System.out.println ("Rest ... ");
}
}//Sunday
;

Day (int ... i) {

}

public abstract void dothing ();

public static Day Gettoday () {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (New Date ());
int i = Calendar.get (Calendar.day_of_week)-1;
switch (i) {
Case 0:
return Day.sun;
Case 6:
return day.sta;
Default
return day.work;
}
}

/**
* Things to do today
*/
public static void Executetodayplan () {
Gettoday (). dothing ();
}
}


Perform:


Results:

Conclusion: This kind of scheme is used to perform differently in several cases which are cured.

Can be used to: such as the loading of multiple data source connections (the program uses MySQL also uses MOGODB, you can use this way to obtain a different data source connection).

Flexible use, avoid mechanically ...

Application of Java Enumeration

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.