Java foundation - enumeration can also be used

Java Basics - enumerations can be used this way

Write in front

When Xiaofu was designing the data access type, he suddenly wanted to divide several things of similar types into one category. It was troublesome to define those constants in Java. I just remembered that there was enumeration. I thought that I didn't use it much, so I summarized the most basic usage, so I had this article

Enumeration concept

What is enumeration?

Enumeration is like Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday in daily life. Usually used to represent a program that lists all members of a poor sequence set.

Enumeration from jdk1 After the 5 update, this new type, enum, came into being.

Common usage of enumeration

Usage 1: constant

Usage 1: constant

Before enumeration, we usually define constants through public static final xxx. After enumeration, many constants related to content can be assigned to an enumeration type, and there are more other methods in the enumeration class.

Code implementation:

public enum Season {
    //spring
    SPRING,
    //summer
    SUMMER,
    //autumn
    AUTUMN,
    //winter
    WINTER
}

Usage 2: switch case statement

Usage 2: switch case statement

I didn't expect it! enum can also appear in our switch case statements, which not only makes the code more readable, but also improves the communication between different developers.

Code implementation:

class SeasonCard{
    public Season getSeason(Season season){
        switch (season){
            case SPRING:
                return Season.SPRING;
            case SUMMER:
                return Season.SUMMER;
            case AUTUMN:
                return Season.AUTUMN;
            case WINTER:
                return Season.WINTER;
            default:
                throw new RuntimeException("Error enumerating contents!");
        }
    }
}

Usage 3: add method to enumeration type

Usage 3: add method to enumeration type

You can define member variables, constructor methods, static methods, etc. in enumeration classes

Code implementation:

public enum Season {
    //spring
    SPRING,
    //summer
    SUMMER,
    //autumn
    AUTUMN,
    //winter
    WINTER;
    
    private long id;
    private Date startDate;
    
    Season() {
    
    }
    
    Season(long id, Date startDate) {
        this.id = id;
        this.startDate = startDate;
    }
    
    public static void getEndDate(){
        System.out.println("End Date : "+ new Date(System.currentTimeMillis() + 1000*3600*24*90L).toString());
    }
    
    public long getId() {
        System.out.println("ID :" + id);
        return id;
    }
    
    public Date getStartDate() {
        System.out.println("Start Date: "+startDate.toString());
        return startDate;
    }
    
}

Usage 4: override methods in enumeration

Usage 4: override methods in enumeration

For example, override the toString() method

public enum Season {
    //spring
    SPRING,
    //summer
    SUMMER,
    //autumn
    AUTUMN,
    //winter
    WINTER;
    
    private long id;
    private Date startDate;
    
    Season() {
    
    }
    
    Season(long id, Date startDate) {
        this.id = id;
        this.startDate = startDate;
    }
    
    @Override
    public String toString() {
        return "Season" + this.id + this.startDate;
    }
}

Usage 5: enumeration can implement interfaces but cannot inherit other classes

Usage 5: enumeration can implement interfaces but cannot inherit other classes

Because it is clearly specified in Java that multiple inheritance is not supported, enumeration types have inherited Java Lang. enum cannot inherit other classes.

Code implementation:

public enum Season implements CardInfo{
    //spring
    SPRING,
    //summer
    SUMMER,
    //autumn
    AUTUMN,
    //winter
    WINTER;
    
    private long id;
    private Date startDate;
    
    Season() {
    
    }
    
    Season(long id, Date startDate) {
        this.id = id;
        this.startDate = startDate;
    }

    @Override
    public String getInfo() {
        return new Customer().getName();
    }
}

interface CardInfo{
    public abstract String getInfo();
}

Usage 6: use interface to organize enumeration

Usage 6: use interface to organize enumeration

Code implementation:

interface CardInfo{
    public abstract String getInfo();
    
    enum VIP implements CardInfo{
        //VIP level
        VIP1,
        VIP2;
    
        @Override
        public String getInfo() {
            return VIP1.name();
        }
    }
    
    enum SVIP implements CardInfo{
        //SVIP level
        SVIP1,
        SVIP2;
    
        @Override
        public String getInfo() {
            return SVIP1.name();
        }
    }
}

Usage 7: use of enumeration collection

Usage 7: use of enumeration collection

java.util.EnumSet and Java util. Enummap is a collection of two enumerations.

The key in EnumMap is enum type and the value is any type.

The difference between the two refers to the JDK8 development documents.

thank

Thank you for your blog

The use cases are designed by Xiaofu

You can see what it means

Write at the end

The above is the relevant usage of enumeration types, which are rarely used in actual development,
Just get familiar with his basic use, because if you don't even know enumeration, it's true
 It's a little easy to attract the attention of peers...

Last last

Or that sentence

Make progress every day and harvest every day

May you succeed in your career and learn

If you feel good, don't forget to click three times~

Keywords: Java Back-end

Added by satre on Sat, 25 Dec 2021 06:37:03 +0200