definition
An
enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.
Enumerated type is a special data type, it is possible to define a set of predefined constants as a variable. One of the variable must equal the value of its predefined.
Enumerated type is part of the new features of Java 5, which is a special type of data, is special because it is a class (class) type but have become more special than the class type of constraint, but these constraints the presence also created the simplicity of enumerated types, safety and convenience.
Enumeration is a special class can have member variables and methods.
use
the Direction enum {public
// semicolon at the end of list of examples is optional
NORTH, SOUTH, EAST, WEST
}
Examples of Direction type can be referenced by way of static variables: Direction.NORTH, Direction.EAST
In the development of enumeration constants can be used to define
@Getter
public enum {TaskStatueEnum
NOTSTARTEND (. 1, "not started"),
EXECUTIONINPROGRESS (2, "execution"),
to COMPLETED (. 3, "completed");
private int val;
private String descript;
TaskStatueEnum(int val, String descript)
{
this.val=val;
this.descript=descript;
}
public static TaskStatueEnum fomart(int val) throws Exception {
for(TaskStatueEnum property: TaskStatueEnum.values())
{
if(val==property.val)
{
return property;
}
}
throw new Exception(MessageFormat.format("找不到对应{0}的任务状态",val));
}
}
Detailed and attention
1> enumeration type implicitly inherited java.lang.Enum class, and therefore can not inherit from other classes, but can implement interfaces;
2> enumerated type only private constructor method (java create runtime, can not be instantiated outside);
3> can not be generic;
4> When there are fields and methods, enumerated list of constants must end with a semicolon;
5> The compiler automatically adds some special methods when creating enumeration. For example, they have a static values
method.
$ javap Color.class Compiled from "Color.java" public final class com.zuoquan.lt.basic.enums.Color extends java.lang.Enum<com.zuoquan.lt.basic.enums.Color> { public static final com.zuoquan.lt.basic.enums.Color RED; public static final com.zuoquan.lt.basic.enums.Color GREEN; public static final com.zuoquan.lt.basic.enums.Color BULE; public static com.zuoquan.lt.basic.enums.Color[] values(); public static com.zuoquan.lt.basic.enums.Color valueOf(java.lang.String); public int getIndex(); public void setIndex(int); public java.lang.String getName(); public void setName(java.lang.String); static {}; }
values method:
Color[] colors = Color.values();
for (Color color : colors) {
System.out.println(color.getName());
}
6> parent Enum Method
S.N. | Method & described |
1 | protected Object clone () method throws an exception CloneNotSupportedException |
2 | int compareTo (E o) of this method is relatively enumeration order specified object. |
3 | Returns true boolean equals (Object other) This method, if the specified object is equal to enumeration constants. |
4 | protected void finalize () This method returns the enumerated classes can not have finalize methods. |
5 | Class getDeclaringClass () This method returns the class object this enumeration constants corresponding to the enumerated type. |
6 | int hashCode () This method returns an enumeration constant hash code. |
7 | String name () method returns the name of this enum constant, precisely because of the statement in its enum declaration. |
8 | int ordinal () This method returns the ordinal number of this enum constant (its position enum declaration, where the initial constant is assigned a sequence number zero). |
9 | String toString () method returns the name of this enum constant, as contained in the statement. |
10 | static> T valueOf (Class enumType, String name) This method returns the specified enum type enum constant with the specified name. |
https://blog.csdn.net/javazejian/article/details/71333103