Java enumeration class summary Enum, java enumeration summary enum

Source: Internet
Author: User

Java enumeration class summary Enum, java enumeration summary enum

I haven't touched the enumeration class for a long time. I almost forgot it. Let's take a moment to summarize it. To be honest, enumeration classes can bring us great convenience.

Note: The enumeration class defines a range. It can be understood that only a fixed number of objects can be generated for external calls. Therefore, the constructor methods in the enumeration class are private. Next, what are the special characteristics of enumeration classes?

Present the code at a Glance (I am also a newbie in the java Android field. If there are any errors, I hope you can give me some advice)

1/** 2 * test enumeration class 3 * @ author Administrator 4 * @ Date 2015-8-3 5 */6 public class EnumTest {7 public static void main (String [] args) {8 Person person1 = Person. MAN; 9 System. out. println (person1); 10 Person. MAN. work (); 11 Person. WOMEN. work (); 12 13} 14 // test the Person's values () method 15 public static void testValues () {16 Person [] persons = Person. values (); 17 for (Person p: persons) {18 19 System. out. println (p); 20 21} 22 23} 24 // test the valuesof () method of Person 25 public static void testValuesOf () {26 Person person2 = Person. valueOf ("MAN"); 27 System. out. println (person2 ); 28} 29} 30/** 31 * test interface 32 * @ author Administrator33 * @ Date 2015-8-334 */35 interface PersonExtends {36 void work (); 37} 38/** 39 * enumeration class 40 * @ author Administrator41 * @ Date 2015-8-342 */43 enum Person implements PersonExtends {44 MAN ("zhangsan ", "12") {45 public void work () {46 System. out. println ("practical, affordable"); 47} 48 49}, WOMEN ("lisi", "13") {50 public void work () {51 52 System. out. println ("white-collar workers in the workplace, where the experts reside"); 53} 54}; 55 private String name; 56 private String age; 57 private Person (String name, String age) {58 this. name = name; 59 this. age = age; 60} 61 public String getName () {62 return name; 63} 64 public void setName (String name) {65 this. name = name; 66} 67 public String getAge () {68 return age; 69} 70 public void setAge (String age) {71 this. age = age; 72} 73 public void method () {74 System. out. println ("The person is liming"); 75} 76 @ Override77 public String toString () {78 // TODO Auto-generated method stub79 return super. toString (); 80} 81 @ Override82 public void work () {83 // TODO Auto-generated method stub84 System. out. println ("men and women work differently"); 85} 86 87}

Several key factors of the above Code are described:

1. I didn't quite understand how to write objects from the beginning of an enumeration class. It was actually very simple, just the abbreviation of the original class. See the following code:

private final Person2 MAN = new Person2("zhangsan","12");

In fact, the objects in the enumeration class are the same as this method, but they cannot be written in the class declared by enum. It can be understood that, since the enumeration class has a limited range, it can only be a new object in the enumeration class. Therefore, the constructor is of the private type and cannot be modified, so it remains unchanged, therefore, the Declaration is of the final type and object initialization, which we all know. However, this Convention is already known in enum, so the enum class does not allow this write, and the default write is deleted, a bit similar to the final static in the interface. So there is MAN ("zhangsan", "12 "). Note that enumeration objects must be written at the beginning of the class.

2. In the test enumeration class, you can see the objects that reference the enumeration class at a glance. There are also two methods to illustrate. One is the values method, which gets the object of the enumeration class defined in the enumeration class and returns a set of enumeration classes. Traverse in sequence as above... The second is the valuesof method, which obtains enumeration Objects Based on names. It is emphasized that the parameters in valuesof (String str) must be the names of objects in the enumeration class. Otherwise, an exception is reported.

It's a bit late today .. Update again tomorrow ..

 

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.