enum class

An enumeration class is essentially a class, but it has some characteristics of its own compared to ordinary classes.

1. Enumeration classes defined by enum inherit from java.lang.Enum by default, so custom enumeration classes cannot explicitly inherit other classes.
2. The non-abstract enumeration class defined by enum is decorated with final by default, which means that it cannot have subclasses.
3. The constructor of an enumeration class can only be modified with private. If it is not explicitly pointed out, it is also private by default, which means that the enumeration class does not allow external instances to be created.
4. All instances of the enumeration class must be explicitly listed on the first line of the enumeration class, and these instances are modified by public static final by default, which means that the instance is immutable once determined, and can be directly passed through the enumeration class name. Obtain.

A super simple enum class:

enum Season {
    SPRING, SUMMER, FALL, WINTER;
}

Here are some uses of the enum class:

1. When enumerating an enumeration instance, you can pass parameters like a constructor (provided there is a constructor with parameters)
enum Season {
    SPRING("春天"), SUMMER("夏天"), FALL("秋天"), WINTER("冬天");
    private String dec;

    private Season(String dec) {
        this.dec = dec;
    }

    public void getDec() {
        System.out.println(dec);
    }
}


2. The enumeration class implements the interface
interface Dec {
    void getDec();
}

It can be implemented like this: (all instances have the same behavior)

enum Season implements Dec {
    SPRING("春天"), SUMMER("夏天"), FALL("秋天"), WINTER("冬天");
    private String dec;

    private Season(String dec) {
        this.dec = dec;
    }

    public void getDec() {
        System.out.println(dec);
    }
}

It can also be like this: (different instances behave differently)

enum Season implements Dec {
    SPRING("春天") {
        public void getDec() {
            System.out.println("111");
        }
    },
    SUMMER("夏天"){
        public void getDec() {
            System.out.println("222");
        }
    },
    FALL("秋天"){
        public void getDec() {
            System.out.println("333");
        }
    }, 
    WINTER("冬天"){
        public void getDec() {
            System.out.println("444");
        }
    };
    private String dec;

    private Season(String dec) {
        this.dec = dec;
    }
}


3. Enumeration class defines abstract methods
enum Season {
    SPRING("春天") {
        public void getDec() {
            System.out.println("111");
        }
    },
    SUMMER("夏天"){
        public void getDec() {
            System.out.println("222");
        }
    }, 
    FALL("秋天"){
        public void getDec() {
            System.out.println("333");
        }
    }, 
    WINTER("冬天"){
        public void getDec() {
            System.out.println("444");
        }
    };
    private String dec;

    private Season(String dec) {
        this.dec = dec;
    }

    public abstract void getDec();

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325077111&siteId=291194637