Crazy Java Note-enum class

Source: Internet
Author: User

1 //To implement an enumeration class manually2 //Steps31. Hide the constructor with private42. Use public for all possible instances of this classStaticFinal-Modified class variable to hold53if necessary, you can provide some static methods that allow other programs to obtain matching instances based on specific parameters6  Public classseason{7     //define the season class as immutable and define its field as final8     Private FinalString name;9     Private FinalString desc;Ten      Public Static FinalSeason SPRING One=NewSeason ("Spring", "Find Spring"); A      Public Static FinalSeason SUMMER -=NewSeason ("Summer", "scorching"); -      Public Static FinalSeason FALL the=NewSeason ("Autumn", "Air cool"); -      Public Static FinalSeason WINTER -=NewSeason ("Winter", "Snow viewing"); -      Public StaticSeason Getseason (intSeasomnim) { +         Switch(seasonnum) { -              Case: 1 +                 returnSPRING; A                  Break; at              Case: 2 -                 returnSUMMER; -                  Break; -Caes:3 -                 returnFALL; -                  Break; in              Case: 4 -                 returnWINTER; to                  Break; +             default: -                 return NULL; the         } *     } $     //define the constructor as private access rightsPanax Notoginseng     PrivateSeason (String name,string desc) { -          This. name=name; the          This. desc=desc; +     } A     //getter methods are provided only for name and Desc the      PublicString GetName () { +         return  This. Name; -     } $      PublicString GetDesc () { $         return  This. Desc; -     } - } the  - //test MethodWuyi  Public classseasontest{ the      PublicSeasontest (Season s) { -System.out.println (s.getname+ Wu S.getdesc ()); -     } About      Public Static voidMain (string[] args) { $         //fall constants that use season directly represent a season object -         Newseasontest (season.fall); -     } - } A //use simple static constants to represent +  Public Static Final intSeason_spring=1; the  Public Static Final intseason_summer=2; -  Public Static Final intSeason_fall=3; $  Public Static Final intSeason_winter=4; the  the Enumerate class instances: the  Public enumseasonenum{ the     //4 enumerated instances in the first row - Spring,summer,fall,winter; in } the  the //Test Class About  Public classenumtest{ the      Public voidjudge (Seasonenum s) { the         //the expression in the switch statement can be an enumeration class the         Switch(s) { +              CaseSPRING: -System.out.println ("Spring"); the                  Break;Bayi              CaseSUMMER: theSystem.out.println ("Summer"); the                  Break; -              CaseFALL: -System.out.println ("Fall"); the                  Break; the              CaseWINTER: theSystem.out.println ("Winter"); the                  Break; -             default: theSYSTEM.OUT.PRINTLN ("Error"); the         } the     } 94      Public Static voidMain (string[] args) { the         //all enum classes have the values method, returning all instances of the enumeration class the          for(Seasonenum s:seasonenum.values ()) { the System.out.println (s);98         } About         //usually when enumerating instances are used, they are always accessed by enumclass.variable. -         Newenumtest (). Judge (seasonenum.spring);101     }102}

Crazy Java Note-enum class

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.