Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

In the last article, we looked at the decompilation source of enumeration classes. In this article, we will talk about the specific rules for using enumeration classes!

Enum

Enum is the common base class for all Java language enumeration types, implementing the Comparable and Serializable interfaces. It contains properties of final type name and ordinal (the ordinal of this enumerated constant, starting from 0). Let’s look at its methods

  • Protected Enum(String name, int ordinal); — Construction method;
  • Public String toString (); — Returns the name field, which enumerates the string that defines the enumeration variable;
  • Protected final Object clone(); – throw CloneNotSupportedException anomaly, guarantee the enumeration class will never be cloned;
  • Public final Class getDeclaringClass(); — Returns the class object corresponding to the enumeration type of this enumeration constant;
  • Protected final void finalize(); Enumeration classes cannot have Finalize methods;
  • ReadObject (ObjectInputStream in); & readObjectNoData (); Throw InvalidObjectException to prevent default deserialization;

extension

  1. Properties can be customized in enumerated classes

    Custom property values are best decorated with private final to prevent the generated set method from modifying property values when used, making the code more secure.

  2. You can customize constructors in enumerated classes

    Constructors must be private to prevent such objects from being declared elsewhere.

  3. Enumeration classes can define custom methods, and enumerations can optionally override custom methods.

    public enum OrderStatus{
        NO_PAY("Unpaid".0),
        PAY("Paid".1) {@Override
            public void printOrderStatus(a) {
                System.out.println("Paid");
            }
        },
        REFUNDING("Refund in progress".2),
        REFUNDED("Refund successful".3),
        FAIL_REFUNDED("Refund failed".4),;private final String name;
        private final int status;
    
        private OrderStatus(String name,int status){
            this.name = name;
            this.status = status;
        }
    
        public void printOrderStatus(a){
            System.out.println("Print order status"); }}public class EnumTest {
        public static void main(String[] args) { OrderStatus.PAY.printOrderStatus(); OrderStatus.NO_PAY.printOrderStatus(); }}Copy the code

Enumeration classes can also have abstract methods, but the enumeration item must override that method.

  1. Enumeration classes implement interfaces

    As with normal classes, interfaces need to be implemented with abstract methods of the interface, and you can have different objects of an enumerated class implement different behaviors.

case

// Define an interface
public interface Order {
    void printOrderStatus(a);
}

// Enumeration classes implement this interface
public enum OrderStatus implements Order{
    NO_PAY("Unpaid".0) {@Override
        public void printOrderStatus(a) {
            System.out.println("Unpaid");
        }
    },
    PAY("Paid".1) {@Override
        public void printOrderStatus(a) {
            System.out.println("Paid");
        }
    },
    REFUNDING("Refund in progress".2) {@Override
        public void printOrderStatus(a) {
            System.out.println("Refund in progress");
        }
    },
    REFUNDED("Refund successful".3) {@Override
        public void printOrderStatus(a) {
            System.out.println("Refund successful");
        }
    },
    FAIL_REFUNDED("Refund failed".4) {@Override
        public void printOrderStatus(a) {
            System.out.println("Refund failed"); }};private final String name;
    private final int status;

    private OrderStatus(String name,int status){
        this.name = name;
        this.status = status; }}Copy the code

If you look at the compiled file at this point, you will see that in addition to the orderStatus.class file, several.class files are generated:

These are files for the anonymous inner classes generated in orderStatus.class.

Now that we know the rules for using enumerated classes, we can use them to implement a small example of order state transitions. If you have different opinions or better idea, welcome to contact AH Q, add AH Q can join the technical exchange group to participate in the discussion!