亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Java JavaBase what is java enumeration

what is java enumeration

Nov 09, 2019 am 11:06 AM
java enumeration

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 Enum>, 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 of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72