Java Enumeration Class Summary Enum

Source: Internet
Author: User

Long time no contact enumeration class, almost forget, today take a moment to summarize it. To be honest, enumerating classes can really make a lot of sense for us.

Description: Enum class It contracts a scope, it can be understood that only a fixed number of objects can be generated to call the outside world, so the constructor method in the enumeration class is private type. Secondly, what is the special point of enumerating classes?

Submit Code at a glance (I am also a novice in the Java Android field, if there is a mistake, I hope you can give advice)

1 /**2 * Test Enumeration class3  * @authorAdministrator4 * @Date 2015-8-35  */6  Public classEnumtest {7      Public Static voidMain (string[] args) {8Person Person1 =Person.man;9 System.out.println (person1);Ten Person.MAN.work (); One Person.WOMEN.work (); A          -     } -     //Test person's values () method the      Public Static voidtestvalues () { -person []persons =person.values (); -          for(person p:persons) { -              + System.out.println (p); -              +         } A              at     } -     //Test person's valuesof () method -      Public Static voidtestvaluesof () { -Person Person2 = person.valueof ("Mans")); - System.out.println (person2); -     } in } - /** to * Test Interface +  * @authorAdministrator - * @Date 2015-8-3 the  */ * Interfacepersonextends{ $     voidWork ();Panax Notoginseng } - /** the * Enum class +  * @authorAdministrator A * @Date 2015-8-3 the  */ + enumPersonImplementspersonextends{ -Man ("Zhangsan", "12"){ $          Public voidWork () { $System.out.println ("capable of physical activity, can withstand weight"); -         } -          the},women ("Lisi", "13"){ -          Public voidWork () {Wuyi              theSystem.out.println ("The majority of workers in the workplace, who can live"); -         } Wu     }; -     PrivateString name; About     PrivateString age; $     PrivatePerson (String name,string age) { -          This. Name =name; -          This. Age =Age ; -     } A      PublicString GetName () { +         returnname; the     } -      Public voidsetName (String name) { $          This. Name =name; the     } the      PublicString getage () { the         returnAge ; the     } -      Public voidsetage (String age) { in          This. Age =Age ; the     } the      Public voidmethod () { AboutSystem.out.println ("The person is Liming"); the     } the @Override the      PublicString toString () { +         //TODO auto-generated Method Stub -         return Super. toString (); the     }Bayi @Override the      Public voidWork () { the         //TODO auto-generated Method Stub -System.out.println ("Men and women work in different intensities"); -     } the      the}

Explain some of the key elements of my code above:

1. It was not quite clear how the first object in the enumeration class would be written, but it was simply an abbreviation for the original class. Look at the following code:

Private Final New Person2 ("Zhangsan", "12");

In fact, the object in the enumeration class is the same as this one, except that it cannot be written in the class of the enum declaration. It can be understood that since the enumeration class has been scoped, only in the enumeration class of the new object, so the construction method is private type, the outside world is not allowed to modify, so it is unchanged, so the declaration becomes the final type, object initialization, which I believe we all know. In enum, however, this convention is known, so the enum class does not allow this to be written, and the default is erased, somewhat like the final static in the interface. So the man is left ("Zhangsan", "12"). As a point of emphasis, the enumeration object must be written at the beginning of the class.

2. In the Test enumeration class, the object that references the enumeration class believes that you can see it in a glance. There are also two ways to illustrate that one is the values method, which gets the object of the enumeration class defined in the enumeration class, and returns the collection of an enumeration class. As on the loop ... Next is the Valuesof method, which gets the object of the enumeration type according to the name, emphasizing that the arguments in the valuesof (String str) must be the names of the objects in the enumeration class, otherwise the exception will be reported.

It's a little late today. We'll do the update tomorrow.

Java Enumeration Class Summary Enum

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.