A brief analysis of the definition and use of enumeration types in Java programming

  • 2020-05-09 18:42:53
  • OfStack

When you define an enumerated type, you are essentially defining a class, but the compiler fills in the details for you, so to some extent the enum keyword ACTS like class or interface.

When you use "enum" define enum type, in essence you defined the types of inherited from java. lang. Enum class, and each member of the enumeration is you define an instance of an enum type (Instance), they are by default final, so you can't change them, they are also static members, so you can use them through the type name directly, of course the most important, they are in the open (public).

Here's an example:


OpConstants.java
public enum OpConstants {TURN_LEFT, TURN_RIGHT, SHOOT} 

. In this case, the OpConstants inherited from java lang. Enum, each enumeration members such as TURN_LEFT, TURN_RIGHT, SHOOT, they are all OpConstants a object instance, is the object instance, it naturally has 1 some methods can be used, for example toString () method is covered, allows you to directly obtain the enumeration values of string description, and enumeration object definition values () method allows you to get all the enumerated instances, and returned as an array way, You use these two methods to simply display the contents of OpConstants:


ShowEnum.java
public 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's ok not to write toString(). The result is as follows:


TURN_LEFT
TURN_RIGHT
SHOOT

You can use the "==" or equals() method to compare enumeration objects, ""==" will compare whether the enumeration objects you provide are the same (that is, occupy the same memory location), and equals() will essentially compare the contents of two enumeration objects, by default, based on the string value of the enumeration.

The valueOf() method lets you try to convert a specified string to an enumeration instance. You can use the compareTo() method, which compares the order of two enumeration objects when they are enumerated, as shown below


ShowEnum.java
public 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_RIGHT
1
0
-1

Returns a positive value, indicating the order before the enumeration object being compared, a negative value after, and a 0 indicating that the positions of the two mutually proportional enumeration values are the same.

For each enumeration member, we can use the ordinal() method to get the location index in enumeration order, starting at 0 by default, for example:


ShowEnum.java
public 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_LEFT
1 TURN_RIGHT
2 SHOOT

The enum keyword can be used to define an enumeration class. You can write related constants in one class. Let's look at another example.

The following code is              


class TestClass {

  private TestClass(){}// Define a private constructor and do not instantiate objects from outside 

  // Provide two instances A , B

  public static final TestClass A=new TestClass();

  public static final TestClass B=new TestClass();

}

You can use enumerated types instead:


enum TestClass01{

  A,B;

}

Usage:

An enumeration can also have constructors, fields, and methods:


ShowEnum.java
public class ShowEnum {
  public static void main(String[] args) {
    for(OpConstants constant: OpConstants.values()) {
      System.out.println(constant.toString());
    }
  }
} 
0

Enumeration can also have abstract methods:


ShowEnum.java
public class ShowEnum {
  public static void main(String[] args) {
    for(OpConstants constant: OpConstants.values()) {
      System.out.println(constant.toString());
    }
  }
} 
1

Test method:


ShowEnum.java
public class ShowEnum {
  public static void main(String[] args) {
    for(OpConstants constant: OpConstants.values()) {
      System.out.println(constant.toString());
    }
  }
} 
2

Operation results:


a

Author: Things start 
Sign You are not a failure as long as you are trying. 


Related articles: