When we want to implement a day of the week enumeration, We will write a class to implement it ourselves! The code for using the object class is as follows:
Package COM. jemsn. weekday; public class weekday {// privatize the constructor so that others cannot create new object instance private weekday () {} public static final weekday Monday = new weekday (); public static final weekday tuesay = new weekday (); public static final weekday wendsday = new weekday (); public static final weekday Thusday = new weekday (); public static final weekday Friday = new weekday (); public static final weekday staday = new weekday (); Pub LIC static final weekday Sunday = new weekday (); // if we want a nextday () method public weekday nextday () {If (this = Monday) {return tuesay ;} else if (this = tuesay) {return wendsday;} else if (this = wendsday) {return Thusday;} else if (this = Thusday) {return Friday ;} else if (this = Friday) {return staday;} else if (this = staday) {return Sunday;} else {return Monday ;}} // override the tostring method Public String tostring () {r Eturn this = Monday? "Monday": This = tuesay? "Tuesday": This = wendsday? "Wednesday": This = Thusday? "Thursday": "Friday ";}}
Then we call it in the test code:
package com.jemsn.weekday;public class test { public static void main(String[] args) {WeekDay monday=WeekDay.MonDay;WeekDay tuesday=WeekDay.Tuesay;System.out.println(monday.NextDay());System.out.println(tuesday.NextDay());}}
The output result is as follows:
-----------------------------------------------------------------------
Tuesday
Wednesday
-----------------------------------------------------------------------
The above Code shows that a lot of if else if and so on are used in our nextday method. If we change the base to an abstract class, the following code:
Package COM. jemsn. weekday; public abstract class weekday {// privatize the constructor so that others cannot create new object instance private weekday () {} public static final weekday Monday = new weekday () {public weekday nextday () {return weekday. tuesay ;}}; // public static final weekday tuesay = new weekday () {public weekday nextday () {return weekday. wendsday ;}}; public static final weekday wendsday = new weekday () {public weekday nextda Y () {return weekday. thusday ;}}; public static final weekday Thusday = new weekday () {public weekday nextday () {return weekday. friday ;}; public static final weekday Friday = new weekday () {public weekday nextday () {return weekday. staday ;}}; public static final weekday staday = new weekday () {public weekday nextday () {return weekday. sunday ;}}; public static final weekday Sunday = new weekday () {public Weekday nextday () {return weekday. monday ;}}; // if we want a nextday () method public abstract weekday nextday (); // rewrite tostring method Public String tostring () {return this = Monday? "Monday": This = tuesay? "Tuesday": This = wendsday? "Wednesday": This = Thusday? "Thursday": "Friday ";}}
In the above Code, if else is converted into independent classes, and the test code remains unchanged. The output result is as follows:
-----------------------------------------------------------------------
Tuesday
Wednesday
-----------------------------------------------------------------------
We can see the same thing, but the implementation method is completely different! So we can sum up using abstract methods combined with anonymous internal classes to achieve better than unified!