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)