Home >Java >javaTutorial >Detailed summary of the use of java enumerations

Detailed summary of the use of java enumerations

WBOY
WBOYforward
2022-03-30 18:19:082426browse

This article brings you relevant knowledge about java, which mainly introduces issues related to the use of enumerations, including the concept of enumerations, the advantages and disadvantages of enumerations, and the Here are some common methods, I hope it will be helpful to everyone.

Detailed summary of the use of java enumerations

Recommended study: "java tutorial"

1. Basic concepts

Enumeration is Java1.5 The new feature introduced is to define enumeration classes through the keyword enum. An enumeration class is a special class. Like ordinary classes, it can use constructors, define member variables and methods, and can also implement one or more interfaces. However, enumeration classes cannot inherit other classes.

2. Advantages and Disadvantages of Enumeration

1. Advantages

Effctive In Java, it is recommended to use enumerations to replace all constant Codes for the following reasons :

(1) Type check, validity check

(2) Enumeration, as a class, can have its own attributes (usually should be constants, I have never encountered a situation where this is not the case) ) and your own method (otherwise you can only use switch to write, which actually violates the principle)

(3) Compared with constants, you can directly know all possible return values ​​without looking at the documentation and source code, which is convenient for coding.

However, the problem here lies in the first point. In fact, (1) is not inevitable in a distributed environment. If the return value of an interface is allowed to have undefined content in business processing, then no exception should be thrown during deserialization, and there is no need to stick to (1). At the same time, judging from points (2) and (3), the impact of limiting the scope of use of enumerations is huge. Rewriting the enumeration that has its own attributes and methods into a combination of code and other methods requires a lot more code, and the degree of code corruption is greatly increased.

2. Disadvantages

(1) Since Java supports single inheritance, enumeration types cannot inherit other classes;

(2) In fact, everyone knows that if the client and server versions are inconsistent, it will cause deserialization exceptions. Therefore, the "Alibaba JAVA Development Manual" handles this problem by avoiding it as much as possible. An exception occurs, so it is forbidden to define enumerations as return values.

3. Solve ifelse

For business development, the complexity of business logic is inevitable. As the business develops, the requirements will only become more and more complex. In order to consider various In this case, there will inevitably be a lot of if-else in the code.

Once there are too many if-else in the code, it will greatly affect its readability and maintainability, and the code will appear very low.
Detailed summary of the use of java enumerations
Enumeration can solve this problem;

Regarding enumeration and switch, it is a relatively simple topic. When using switch for conditional judgment, the conditional parameters can generally only be integers and characters. type. The enumeration type is indeed supported by switch. After java 1.7, switch also supports strings. Here we briefly look at the use of switch and enumeration types

static void testSwitch(Week week){
    switch (week){
        case MONDAY:
            System.out.println(week.getMeaning());
            break;
        case TUESDAY:
            System.out.println(week.getMeaning());
            break;
        case WEDNESDAY:
            System.out.println(week.getMeaning());
            break;
        case THURSDAY:
            System.out.println(week.getMeaning());
            break;
        case FRIDAY:
            System.out.println(week.getMeaning());
            break;
        case SATURDAY:
            System.out.println(week.getMeaning());
            break;
        case SUNDAY:
            System.out.println(week.getMeaning());
            break;
        default:
            System.out.println("您输入有误");
            break;
    }}

IV. Common methods of enumeration

Detailed summary of the use of java enumerations

## Recommended learning: "

javalearningtutorial

The above is the detailed content of Detailed summary of the use of java enumerations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete