<JAVA> enum type (enum)

Source: Internet
Author: User

Enumeration understands:

The enumeration type is part of the new feature in Java 5, which is a special type of data that is special because it is a class type but has some special constraints over the class type, but

The existence of these constraints also creates the simplicity, security, and convenience of enumeration types.

Keyword: enum.

 Public enum Color {    RED, GREEN, BLANK, YELLOW  }  

To add a new method to the enumeration

 Public enumColor {RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables    PrivateString name; Private intindex; //Construction Method    PrivateColor (String name,intindex) {           This. Name =name;  This. index =index; }      //Common Methods     Public StaticString GetName (intindex) {           for(Color c:color.values ()) {if(C.getindex () = =index) {                  returnC.name; }          }          return NULL; }      //Get Set Method     PublicString GetName () {returnname; }       Public voidsetName (String name) { This. Name =name; }       Public intGetIndex () {returnindex; }       Public voidSetindex (intindex) {           This. index =index; }  }

Implementing interfaces

All enumerations are inherited from the Java.lang.Enum class. Because Java does not support multiple inheritance, enumeration objects can no longer inherit from other classes.

 Public InterfaceBehaviour {voidprint ();  String GetInfo (); }   Public enumColorImplementsbehaviour{RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables    PrivateString name; Private intindex; //Construction Method    PrivateColor (String name,intindex) {           This. Name =name;  This. index =index; }  //interface Method@Override PublicString GetInfo () {return  This. Name; }      //interface Method@Override Public voidprint () {System.out.println ( This. index+ ":" + This. Name); }  }  
common methods for enum abstract classes

return type Method Name Method Description
int compareTo(E o) Compare 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.
Class<?> getDeclaringClass() Returns the Class object that corresponds to the enumeration type of this 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 that is contained in the Declaration
static<T extends Enum<T>> T static valueOf(Class<T> enumType, String name) 返回带指定名称的指定枚举类型的枚举常量。

<JAVA> enum type (enum)

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.