Use of enumerations for new features of Java 28-5 JDK5

Source: Internet
Author: User

The previous chapter, customizing the enumeration class, is super troublesome.

So, Java gives an enumeration class: Class Enum<e extends Enum<e>>

  

Precautions

    • Defining enum classes to use the keyword enum
    • All enum classes are subclasses of enum
    • The first line of an enumeration class must be an enumeration item, and the semicolon after the last enumeration item can be omitted, but the semicolon cannot be omitted if there is something else in the enumeration class. It is recommended not to omit
    • An enumeration class can have a constructor, but it must be private, and it defaults to private. The use of enumeration items is special: enumeration ("");
    • An enumeration class can also have an abstract method, but an enumeration item must override the method
    • Use of enumerations in a switch statement

Methods of enum:

In addition to the following methods, there is a method that cannot be found in the API:

  Values (); : Each enumeration class has this method that iterates through all the beauty values of the enumeration class.

1         // values () 2         // Although this method is not found in the JDK document, each enumeration class has that method, and it is convenient to traverse all enumerated values of the enumeration class 3         direction2[] dirs = direction2.values (); 4          for (Direction2 d2:dirs) {5            System.out.println (D2); 6             System.out.println (D2.getname ()); 7         }

Method Summary
protected  Object clone()
Throws Clonenotsupportedexception.
 int compareTo(E o)
Compares this enumeration with the order of the specified objects.
 boolean equals(Object other)
Returns True when the specified object is equal to this enumeration constant.
protected  void finalize()
Enum classes cannot have a finalize method.
 Class<E> getDeclaringClass()
Returns the Class object that corresponds to the enumeration type of this enumeration constant.
 int hashCode()
Returns the hash code for the enumeration constant.
 String name()
Returns the name of this enumeration constant, declared in its enumeration declaration.
 int ordinal()
Returns the ordinal of an enumeration constant (its position in the enumeration declaration, where the initial constant ordinal is 0).
 String toString()
Returns the name of the enumeration constant, which is contained in the Declaration.
static
<T extends Enum<T>>
T
valueOf(Class<T> enumType, String name)
Returns an enumeration constant of the specified enumeration type with the specified name.

First edition:

 Public enum Direction01 {    FRONT, benind, left, right;}

Second Edition:

1  Public enumDIRECTION02 {2 3FRONT ("front"), Benind ("Back"), left ("Ieft"), right ("R");4 5     PrivateString name;6 7     PrivateDirection02 (String name) {8          This. Name =name;9     }Ten  One      PublicString getname () { A         returnname; -     } -}

Third edition:

1  Public enumDirection03 {2FRONT ("front") {3 @Override4          Public voidShow () {5System.out.println ("front");6 7         }8     },9BEHIND ("after") {Ten @Override One          Public voidShow () { ASystem.out.println ("after"); -  -         } the     }, -Left ("Ieft") { - @Override -          Public voidShow () { +System.out.println ("left"); -  +         } A     }, atRight ("R") { - @Override -          Public voidShow () { -System.out.println ("Right"); -  -         } in     }, ; -     PrivateString name; to  +     PrivateDirection03 (String name) { -          This. Name =name; the     } *  $      Publicstring GetName (string name) {Panax Notoginseng         returnname; -     } the  +      Public Abstract voidShow (); A}

Test class:

1  Public classDirectiondemo {2 3      Public Static voidMain (string[] args) {4 5         //Print first version6Direction01 D1 =Direction01.front;7System.out.println (D1);//FRONT8 9         //Print second versionTenDIRECTION02 D2 =Direction02.front; OneSystem.out.println (D2);//FRONT ASystem.out.println (D2.getname ());//before -  -         //Print a third version (enumeration used in a switch statement) theDIRECTION03 D3 =Direction03.front; -  -         Switch(D3) { -          CaseFRONT: +System.out.println ("Pre-selected"); -              Break; +  A          CaseBEHIND: atSystem.out.println ("After selected"); -              Break; -  -          CaseLeft : -System.out.println ("Left selected"); -              Break; in  -          CaseRight : toSystem.out.println ("Select Right"); +              Break; -  the         default: *              Break; $         }Panax Notoginseng  -     } the  +}

Use of enumerations for new features of Java 28-5 JDK5

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.