Home>Article>Java> what is java enumeration

what is java enumeration

藏色散人
藏色散人 Original
2019-11-09 11:06:36 6322browse

what is java enumeration

What is a java enumeration?

java The definition and usage of enumeration

1. The definition of enumeration:

Enumeration is A special data type. The reason why it is special is that it is a class type but has more special constraints than the type. However, the existence of these constraints also makes the enumeration type simple, safe and convenient. . To create an enumeration type, use the enum keyword, which implies that the types created are subclasses of the java.lang.Enum class (java.lang.Enum is an abstract class). The enumeration type conforms to the general pattern Class Enum271447d3c75ca4f8ce424bd25b75b2af>, where E represents the name of the enumeration type. Each value of the enumeration type is mapped to the protected Enum(String name, int ordinal) constructor, where the name of each value is converted to a string, and the ordinal setting represents the order in which this setting is created.

2. The use of enumeration:

Create an enumeration class: EnumTest

public enum EnumTest { //星期一,星期二,星期三,星期四,星期五,星期六 MON(1), TUE(2),WED(3),THU(4),FRI(5),SAT(6){ public boolean isRest(){ return true; } }, //星期日 SUN(0){ public boolean isRest(){ return true; } }; private int value; private EnumTest(int value){ this.value=value; } public int getValue(){ return value; } public boolean isRest(){ return false; } }

Use EnumTest enumeration class:

public class EnumMain { public static void main(String[] args) { for (EnumTest enumTest : EnumTest.values()) { System.out.println(enumTest + ":" + enumTest.getValue()); } System.out.println("---------------我是分割线------------"); EnumTest test = EnumTest.SAT; switch (test) { case MON: System.out.println("今天是星期一"); break; case TUE: System.out.println("今天是星期二"); break; case WED: System.out.println("今天是星期三"); break; case THU: System.out.println("今天是星期四"); break; case FRI: System.out.println("今天是星期五"); break; case SAT: System.out.println("今天是星期六"); break; case SUN: System.out.println("今天是星期日"); break; default: System.out.println(test); break; } } }

Result:

MON:1 TUE:2 WED:3 THU:4 FRI:5 SAT:6 SUN:0

---------------I am the dividing line------------

Today is Saturday

3. Advantages and Disadvantages of Enumerations:

Before the emergence of enumerations, if you wanted to represent a specific set of discrete values, you often used some constants . For example:

public class Entity { public static final int VIDEO = 1;//视频 public static final int AUDIO = 2;//音频 public static final int TEXT = 3;//文字 public static final int IMAGE = 4;//图片 private int id; private int type; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getType() { return type; } public void setType(int type) { this.type = type; }

4. Disadvantages of using this constant method:

1. The code has poor readability and usability, due to setType The parameters of the () method are of type int,

2. Type unsafe. When the user calls, the types must be completely consistent and the value range must be correct. Like setType(-1); is legal, but not reasonable, and will cause various problems for the program in the future.

3. High coupling and poor scalability. If, for some reason, the value of a constant in the Entity class needs to be modified, it is no joke if the modification is missed when the modification is needed.

Enumerations were born for such problems. They give the ability to compare one arbitrary term with another.

The above is the detailed content of what is java enumeration. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is java class Next article:what is java class