Brief Introduction to enumeration classes in Java

Source: Internet
Author: User

I. concept in some cases, the objects of a class are limited and fixed. For example, a seasonal class has only four objects. Such instances are finite and fixed classes, it is called an enumeration class in Java. Manual Implementation of enumeration classes: 1. Use private to hide the constructor. 2. Save all possible instances of this class using the public static final attribute. 3. If necessary, you can provide some static methods that allow you to obtain matching instances through specific parameters. See the following example:

Package com. home; public class Season {private final String name; public static final Season SPRING = new Season ("SPRING"); public static final Season SUMMER = new Season ("SUMMER "); public static final Season FALL = new Season ("Autumn"); public static final Season WINTER = new Season ("WINTER "); // define the constructor as private public Season (String name) {super (); this. name = name;} // only provides the getter method public String getName () {return name ;} /*** provides a static method to obtain the instance ** @ param seasonNum * @ return */public static Season getSeason (int seasonNum) {switch (seasonNum) {case 1: return SPRING; case 2: return SUMMER; case 3: return FALL; case 4: return WINTER; default: return null ;}}}

 

Ii. differences from the above example, we can see that it is troublesome to manually define enumeration classes. We can use the enum keyword to define enumeration classes. The differences between enumeration classes and common classes are as follows: 1. the enumeration class defined by enum inherits java by default. lang. instead of inheriting the Object class. Java. lang. Enum class implements java. lang. Serialiable and java. lang. Comparable interfaces. 2. the constructor of the enumeration class can only use the private modifier, and the default value is private even if it is omitted. 3. Enumeration instances can only be displayed and provided. Otherwise, instances will never be generated. When these instances are listed, the system will automatically add public static final modification without the need for programmers to display and Add. 4. All enumeration classes provide a values Method for traversing all enumeration values conveniently. Example of enumeration classes defined using the enum Keyword:
Package com. home; public enum Gender {// The enumerated value here must call the corresponding constructor to create MALE ("MALE"), FEMALE ("FEMALE"); private final String name; // The constructor of the enumeration class can only use private to modify private Gender (String name) {this. name = name;} // only provides the getter method public String getName () {return name ;} /*** provides a static method to obtain the instance description ** @ param gender * @ return */public static String getGenderDisc (Gender gender) {// note that the switch expression can be an enumeration value switch (gender) {case MALE: return "I am a man"; case FEMALE: return "I am a woman"; default: return null ;}}}

 

Iii. Introduction to some common methods of enumeration classes. See the following test classes:
Package com. home; public class TestGender {public static void main (String [] args) {// Gender. values () lists all enumerated values for (Gender g: Gender. values () {System. out. println (g);} // you can directly use EnumClass to obtain the instance. variable form Gender gender = Gender. MALE; System. out. println (Gender. getGenderDisc (gender); // You can also use the Enum valueOf method to obtain the enumerated value System. out. println (Enum. valueOf (Gender. class, "MALE"); // compare the order of enumerated objects. If the enumerated object is located before the specified enumerated object, negative integer-1 is returned. Then, positive integer 1 is returned; otherwise, 0 System is returned. out. println (Gender. FEMALE. compareTo (Gender. MALE); // The name method is similar to the toString method, and returns the name of the enumerated constant. Generally, the toString method is used to System. out. println (Gender. MALE. name (); // ordinal () returns the index value of the enumerated value. The first value is 0, that is, System starts from 0. out. println (Gender. MALE. ordinal ());}}

 

4. The enumeration class that implements the interface is actually not much different from the ordinary class. It also uses the implements keyword to implement one or more interfaces. However, it should be noted that if you directly override the methods in the interface after implementing the interface like a common class, all instances of the enumeration class will have the same interface implementation, in this way, meaning and flexibility are lost, so different methods are often provided for each instance, for example:
Package com. home; public enum Gender implements GenderDesc {MALE ("MALE") {// the braces section is actually a class body section public void info () {System. out. println ("I am a man") ;}}, FEMALE ("FEMALE") {public void info () {System. out. println ("I am a woman") ;}}; private final String name; // The constructor of the enumeration class can only use private to modify private Gender (String name) {this. name = name;} // only provides the getter method public String getName () {return name ;}}

 

Interface:
package com.home;    public interface GenderDesc {      public void info();  }  

 

5. Enumeration classes that contain abstract methods. The use of common methods in enumeration classes is the same as that of normal classes. Here we will only introduce the use of their abstract methods, in fact, the abstract method and the implementation interface are a truth. Let's not talk about it here. Let's look at the example:
Package com. home; public enum Gender {MALE ("MALE") {// The curly braces section is actually a class body section public String getInfo () {return "I am a man" ;}}, FEMALE ("FEMALE") {public String getInfo () {return "I am a woman ";}}; private final String name; // The constructor of the enumeration class can only use private to modify private Gender (String name) {this. name = name ;}// only the getter method public String getName () {return name ;}// defines an abstract method for the enumeration class, this abstract method provides different implementations of public abstract String getInfo () by different enumerated values ();}

 

Note: The enumeration class above Defines abstract methods, but the class does not use abstract modification. This is different from the ordinary class because the enumeration class needs to display the created enumerated value, and provide implementation for the abstract method. Otherwise, a compilation error occurs.

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.