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