No one does not know enumeration!

enumerate

Introduction

JDK1.5 introduced a new type-enumeration.
Before JDK1.5, we defined constants as: public static fianl.... It is difficult to manage.
Enumeration, you can group related constants into an enumeration type, and enumeration provides more methods than constants.
Used to define a limited number of similar constants, such as:
error level:
low, medium, high, urgent
. Four seasons of the year:
spring, summer, autumn, winter
. Type of product:
beauty, mobile phone, computer, men's clothing, women's clothing...

Enumeration definition

The constants defined in the enumeration type are instances of the enumeration type.
Permission modifier enum enumeration name { instance 1, instance 2, instance 3, instance 4; }

Before evolution (before jdk1.5)

Describe constants of the same type

package work.february.ten;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-10 21:11
 * @Modified By:
 */
public class Level {
    
    
    public Level(int levelValue) {
    
    
        this.levelValue = levelValue;
    }

    private int levelValue;

    public void setLevelValue(int levelValue) {
    
    
        this.levelValue = levelValue;
    }

    public int getLevelValue() {
    
    
        return levelValue;
    }


    public static final Level low = new Level(1);
    public static final Level MEDIUM = new Level(50);
    public static final Level HIGH = new Level(100);
}

Evolution version

package work.february.ten;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-10 21:22
 * @Modified By:
 */
public enum Level1 {
    
    
    LOW(1),MEDIUM(50),HIGH(100);
    private int levelValue;

    public int getLevelValue() {
    
    
        return levelValue;
    }

    public void setLevelValue(int levelValue) {
    
    
        this.levelValue = levelValue;
    }

    private Level1(int levelValue) {
    
    
        this.levelValue = levelValue;
    }
}

What it looks like now:

package work.february.ten;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-10 21:25
 * @Modified By:
 */
public enum Level2 {
    
    
    LOW,MEDIUM,HIGH;
}

Main methods of enumeration class

Insert picture description here

Use of interface

package work.february.ten;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-10 21:25
 * @Modified By:
 */
public enum Level2 implements Show {
    
    
    LOW{
    
    
        @Override
        public void show() {
    
    
            System.out.println("低级");
        }
    },MEDIUM{
    
    
        @Override
        public void show() {
    
    
            System.out.println("中级");
        }
    },HIGH{
    
    
        @Override
        public void show() {
    
    
            System.out.println("高级");
        }
    };


}
interface Show{
    
    
   void show();
}

Precautions

Once an enumeration is defined, it is best not to attempt to modify the values ​​in it unless the modification is necessary.
Enumeration class inherits java.lang.Enum class instead of Object class by default.
Enumeration class cannot have subclasses, because its enumeration class is finalized by default. It
can only have private construction method
. When enumeration is used in switch, use constant directly Name, do not need to carry the class name. The
name attribute cannot be defined because the name attribute
is built in. Do not provide a set method for the attributes in the enumeration class, which does not conform to the original design intention of the enumeration.
Insert picture description here

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/113785807