Java Base 42 Enumeration (Class)

Source: Internet
Author: User
Tags modifier

1. Overview

enumeration: Some methods at run time require that data not be arbitrary, but must be a range of values that can be used to resolve

2. The format of the enumeration
enum class Name {     enumeration value}

Example

1 Package   com.dhb.enumeration;2 3 /**4 * @author dshore/2018-6-215  *6  */7 enumsex{//enum class 8     //equivalent: public static final sex man=new sex ("male");9Man"male") {Ten @Override One          Public voidrun () { ASystem. out. println ("men are running ."); -         } -     //equivalent: public static final sex woman=new sex ("female"); the},woman ("female") { - @Override -          Public voidrun () { -System. out. println ("The woman is swimming ."); +         } -     }; + String value; A     PrivateSex (String value) { at          This. value=value; -     } -     //member functions -      Public voidGetValue () { -System. out. println ("Value:"+value); -     } in      Public Abstract voidrun (); - } to  +  Public classDemo2 { -      Public Static voidMain (string[] args) { theSex Sex=sex.man;//get enum Class object *Sex.value="male"; $Sex.getvalue ();//return value: MalePanax NotoginsengSex.run ();//return value: The man is running -     } the}
3. Enumeration to note the details(Summary of the above example)

1. The enumeration class is also a special class.
2. The default modifier for enumeration values is public static final
3. The enumeration value is the type of the class that the enumeration value belongs to, and the enumeration value points to the object of this class
4. Enum class constructor method default modifier private
5. Enumeration classes can define their own member variables and member functions
6. An enumeration class can define its own constructor, and the constructor modifier alone must be private
7. Enumeration classes can have abstract methods, but enumeration values must implement abstract methods
8. The enumeration value must be in the first statement of the enumeration class

4. Example

Requirements: Define the gender of a user cannot be changed/instantiated arbitrarily

Mode 1: (without enumeration)

1 Package   com.dhb.enumeration;2 3 /**4 * @author dshore/2018-6-215  *6  */7 classgender{//Customizing a gender category8 String value;9      Public StaticFinal Gender man=NewGender ("male");Ten      Public StaticFinal Gender woman=NewGender ("female"); One  A     PrivateGender (String value) { -              This. value=value; -     } the } -  - classperson{//Personnel category -     PrivateString name; +     PrivateGender sex; -      PublicString GetName () { +         returnname; A     } at      Public voidsetName (String name) { -          This. Name =name; -     } -      PublicGender Getsex () { -         returnsex; -     } in      Public voidsetsex (Gender sex) { -          This. sex=sex; to     } + } -  the  Public classDemo1 { *      Public Static voidMain (string[] args) { $Person p=NewPerson ();Panax NotoginsengP.setname ("Zhang San"); - P.setsex (Gender.woman); theSystem. out. println ("Name:"+p.getname () +"\ t,"+p.getsex (). value);//return Value: Name: Zhang San, female +     } A}

Mode 2: (with enumeration)

1 Package com.dhb.enumeration;2 3 /**4 * @author dshore/2018-6-215  *6  */7 enumgender{//Enum class8Man"male"), Woman ("female");9 String value;Ten     PrivateGender (String value) { One          This. value=value; A     } - } -  the classperson{//Personnel category -     PrivateString name; -     PrivateGender sex; -      PublicString GetName () { +         returnname; -     } +      Public voidsetName (String name) { A          This. Name =name; at     } -      PublicGender Getsex () { -         returnsex; -     } -      Public voidsetsex (Gender sex) { -          This. sex=sex; in     } - } to  +  Public classDemo1 { -      Public Static voidMain (string[] args) { thePerson p=NewPerson (); *P.setname ("Zhang San"); $ P.setsex (Gender.woman);Panax NotoginsengSystem. out. println ("Name:"+p.getname () +"\ t,"+p.getsex (). value);//return Value: Name: Zhang San, female -     } the}

Original Dshore

Author's homepage:http://www.cnblogs.com/dshore123/

Source:https://www.cnblogs.com/dshore123/p/9210345.html

Welcome reprint, reprint must explain the source. ( If this article is helpful to you, you can click on the lower right corner of the recommendation , or comments, thank you!) )

Java Base 42 Enumeration (Class)

Related Article

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.