"Crazy Java Handout" (16)----enumeration class

Source: Internet
Author: User
Tags modifier

Tag: Instance variable specifies security modifier otherwise constructs test inf

    • The difference between an enumeration class and a normal class
      • An enumeration class can implement one or more interfaces, Enum classes defined with enum inherit the Java.lang.Enum class by default, not the Java.lang.Object class, where the Java.lang.Enum class implements Java.lang.Serializable and Java.lang.Comparable two interfaces.
      • With the enum definition, a non-abstract enum class uses the final adornment by default, so the enumeration class cannot derive subclasses.
      • The constructor for an enumeration class can only use the private access control character
      • All instances of an enumeration class must be explicitly listed in the first row of the enumeration class, or the enumeration class never generates an instance. When these instances are listed, the public static final decoration is automatically added without the programmer being explicitly added.
      • Enumeration classes provide a values method that makes it easy to iterate through all of the enumeration values.

enumseasonenum {SPRING, SUMMER, FALL, WINTER;} Public classEnumtest { Public voidjudge (Seasonenum s) {Switch(s) { CaseSPRING:System.out.println ("Spring is comming");  Break;  CaseSUMMER:System.out.println ("Summer is comming");  Break;  CaseFALL:System.out.println ("Fall is comming");  Break;  CaseWINTER:System.out.println ("Winter is comming");  Break; default: System.out.println ("Not exist season"); }    }     Public Static voidMain (string[] args) { for(Seasonenum s:seasonenum.values ()) {System.out.println (s); }        Newenumtest (). Judge (seasonenum.spring); }}

    • Field/methods/constructors for enumeration classes

Enum classes can define field and method. You can also set the field accessor to public, modifying the value directly, but this destroys the encapsulation of the class. You can also change the field accessor to private to provide getter and setter, but usually the enumeration class should be designed as an immutable class.

The field value of the

Enum class should not be allowed to change, which would be more secure and the code concise. Therefore, the field of the enumeration class uses the private final adornment. Therefore, you must specify the initial value in the constructor for these field (or specify the default value when defining the field, specify the initial value in the initialization block, but these are not common), so you should explicitly define the constructor with parameters for the enumeration class. Once the constructor with parameters is explicitly defined for the enumeration class, the incoming arguments must correspond to the first row out of the enumeration instance.

 Public enum Gender {    MALE ("Nan"), FEMALE ("NV");         Private Final String name;         Private Gender (String name) {        this. Name = name;    }           Public String GetName () {        returnthis. Name;    }}

When enumerating values are listed in an enumeration class, the constructor is actually called to create an enumeration class object, except that there is no need to use the New keyword or explicitly call the constructor. In the previous example, the enumeration values were not passed into the parameter, simply because the preceding enumeration class contained a parameterless constructor.

MALE ("Nan") is equivalent to public static final Gender MALE = new Gender ("Nan");

    • An enumeration class that implements an interface
Interface Genderdesc {    void  info ();}  Public enum Implements genderdesc{    MALE, FEMALE;    @Override    publicvoid  info () {        //  TODO auto-generated method Stub            }}

If the methods in the interface are implemented by an enumeration class, each enumeration class behaves the same way when the method is called. If you need each enumeration value to render a different way of behaving when the method is called, you can have each enumeration value implement the method separately

InterfaceGenderdesc {voidinfo ();} Public enumGender2Implementsgenderdesc{MALE {@Override Public voidinfo () {//TODO auto-generated Method Stub}}, FEMALE {@Override Public voidinfo () {//TODO auto-generated Method Stub                    }    };}

In the upper case, when creating an Male/famale enumeration value, instead of creating an instance of the gender enumeration class directly, it is equivalent to creating an instance of an anonymous subclass of gender.

Compiling the top program, generating Gender.class, Gender$1.class, gender$2.class three files, proving male and female are actually instances of Gender anonymous subclasses, not Gender classes.

Question: is the enum class not final decorated? How can you derive subclasses?

Answer: Not all enum classes use the final modifier, and non-abstract enum classes are defaulted to the final decoration. For an abstract enum class-as long as it contains an abstract method, it is an abstract enumeration class, and the system defaults to abstract, not final.

    • Enumeration classes that contain abstract methods
 Public enumOperation {PLUS { Public DoubleEvalDoubleXDoubley) {returnX +y; }}, minus { Public DoubleEvalDoubleXDoubley) {returnX-y; }}, Times { Public DoubleEvalDoubleXDoubley) {returnX *y; }}, DIVIDE { Public DoubleEvalDoubleXDoubley) {returnX/y;    }    };  Public Abstract DoubleEvalDoubleXDoubley);  Public Static voidMain (string[] args) {System.out.println (Operation.PLUS.eval (3, 4)); System.out.println (Operation.MINUS.eval (3, 4)); System.out.println (Operation.TIMES.eval (3, 4)); System.out.println (Operation.DIVIDE.eval (3, 4)); }}

An enumeration class that defines an abstract method cannot use the abstract keyword to define an enumeration class as an abstract class (because the system automatically adds an abstraction to it), but because an enumeration class needs to explicitly create an enumeration value instead of as a parent class, each enumeration value must be defined to provide an implementation for the abstract method, or a compilation error.

"Crazy Java Handout" (16)----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.