When defining an enum type, you essentially define a class, but many details are filled by the compiler for you, so to some extent, the role of the enum keyword is like class or interface.
When you define an enum type using "enum", the types you define are essentially inherited from the java.lang.Enum class, and the member of each enum is actually an instance of the enum type you define. They are all defaulted to final, so you cannot change them, they are also static members, so you can use them directly through the type name, and of course, most importantly, they are all public.
For example:
OpConstants.javapublic enum OpConstants {TURN_LEFT, TURN_RIGHT, SHOOT} In this example, OpConstants inherits from java.lang.Enum. Each enum member such as TURN_LEFT, TURN_RIGHT, SHOOT. They are all object instances of OpConstants, that is, object instances. There are naturally some methods above. For example, the toString() method is overwritten, which allows you to directly obtain the string description of the enum value. The values () method defined by the enum object allows you to obtain all the enum instances and pass them back in an array. You use these two methods to simply display the content of OpConstants:
ShowEnum.javapublic class ShowEnum { public static void main(String[] args) { for(OpConstants constant: OpConstants.values()) { System.out.println(constant.toString()); } }} Basically, println() will automatically call toString(), so it is actually OK to not write toString(). The execution result is as follows:
TURN_LEFTTURN_RIGHTSHOOT
You can use the "==" or equals() method to compare the enum objects. ""=="" will compare whether the enum objects you provide are the same (that is, occupying the same memory location), while equals() will essentially compare the contents of two enum objects. By default, it will be compared based on the string value of the enum.
The valueOf() method allows you to try to convert the specified string into an enum instance. You can use the compareTo() method, which can compare the order of the two enum objects when enumeration. Here is an example
ShowEnum.javapublic class ShowEnum { public static void main(String[] args) { enumCompareTo(OpConstants.valueOf(args[0])); } public static void enumCompareTo(OpConstants constant) { System.out.println(constant); for(OpConstants c: OpConstants.values()) { System.out.println(constant.compareTo(c)); } }} Execution results:
$java ShowEnum TURN_RIGHT
TURN_RIGHT10-1
Pass back a positive value, indicating that the order is before the enum object being compared, after the negative value is indicated, and 0 means that the positions of the two mutually comparative enum values are the same.
For each enumeration member, we can use the ordinal() method to get the position index in the enumeration order, and the default starts with 0, for example:
ShowEnum.javapublic class ShowEnum { public static void main(String[] args) { for(OpConstants c : OpConstants.values()) { System.out.printf("%d %s%n", c.ordinal(), c); } }} Execution results:
0 TURN_LEFT1 TURN_RIGHT2 SHOOT
The enum keyword can be used to define an enum class, and the relevant constants can be written in a class. Let's take a look at an example below.
The following code:
class TestClass { private TestClass(){}//Define a private constructor, and cannot instantiate an object from the outside//Provide two instances A and B public static final TestClass A=new TestClass(); public static final TestClass B=new TestClass();}You can use enum types instead:
enum TestClass01{ A,B;}usage:
An enum can also have constructors, fields, and methods:
enum TestClass02{ A("a") //When creating an object, pass in constructor parameters, B("b"); private String value; private TestClass02(String value){ this.value=value; } public String getValue(){ return this.value; }}Enumerations can also have abstract methods:
enum TestClass03{ A(){ //Implement abstract method public void type(){ System.out.println("Excellent"); } } ,B(){ public void type(){ System.out.println("Good"); } }; public abstract void type();//Define abstract method}Test method:
public class Test02 { public static void main(String[] args){ print(TestClass02.A);//Pause in enum parameters} public static void print(TestClass02 t){ System.out.println(t.getValue()); }enum TestClass02{ A("a") ,B("b"); private String value; private TestClass02(String value){ this.value=value; } public String getValue(){ return this.value; }}}Running results:
aAuthor:Start Sign: As long as you are still trying, it is not a failure.