Comparison between ordinary class implementation enumeration and abstract class applications

Source: Internet
Author: User

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!

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.