Detailed explanation of enumerated types and the use of mysql -jpa

 

Transfer:  https://blog.csdn.net/u012867993/article/details/112372335?share_token=3eb6b458-93e0-4297-bb94-20f8f1cd9576

 

Introduction: An
enumeration type is a special data type that can define a set of predefined constants for a variable. The variable must be equal to one of its predefined values. There was no enumeration type before JDK 1.5. At that time, interface constants were generally used instead. The Java enumeration type enum can be used to express this kind of constant more closely. The advantage of this is to classify the constants and make them more meaningful. You must use the enum keyword when declaring an enumeration, and then define the name, accessibility, basic types, and members of the enumeration.
1. The definition method and explanation of simple enumeration
Define the enumeration type, which means the seasons of the year. code show as below:

public enum FourSeasons {
	spring,//春
	summer,//夏
	autumn,//秋
	winter//冬
}

 

 

From the above code, we can see that we have defined an enumeration class FourSeasons, which has four attributes: spring, summer, autumn, winter. We can use the attributes of the enumeration class to operate like constants, as follows:

public class EnumTest {
	
	public static void main(String[] args) {
		System.out.println(FourSeasons.spring==FourSeasons.summer);
	}
}

 

The output result is:
Insert picture description here
Next, we define a test class and take a look at the data structure of this enumeration class, as follows:

public class EnumTest {
	
	public static void main(String[] args) {
		for(FourSeasons forSeasons:FourSeasons.values()) {
			System.out.println("当前的季节是:"+forSeasons.name()+" 下标为:"+forSeasons.ordinal());
		}
	}

}

Output the following log.
Insert picture description here
It can be seen that each element of the enumeration has at least two attributes, name and ordinal (subscript), and the default subscript starts from 0. The direct comparison of enumerations is actually to compare whether the subscripts of the two are the same. This comparison is higher than that of String.
Second, define the description information of the enumeration.
As in the above code, we define the four seasons of spring, summer, autumn and winter, but only define the type of enumeration, without copying each element. In real development, if you want to use enumeration instead of Stirng type Constants, we also need to modify the following


public enum FourSeasons {
	spring("春"),//春
	summer("夏"),//夏
	autumn("秋"),//秋
	winter("冬");//冬
	
	//定义描述信息
	private String description;
	//构造方法,将描述信息加入到元素中
	private FourSeasons(String description){
		this.description=description;
	}
	//获取描述信息
	public String getDescription() {
		return description;
	}
	
}

Next we print the description information of the enumeration

public class EnumTest {
	
	public static void main(String[] args) {
		for(FourSeasons forSeasons:FourSeasons.values()) {
			System.out.println("当前的季节是:"+forSeasons.name()+" 下标为:"+forSeasons.ordinal()+", 描述为:"+forSeasons.getDescription());
		}
	}

}

Since we have defined the getDescription method, we can directly call the getDescription method in the test class to obtain the description information of the enumeration class. If there are multiple parameters, multiple variables can be defined and added to the constructor. as follows:

public enum FourSeasons {
	spring("春天","桃花开"),//春
	summer("夏天","荷花开"),//夏
	autumn("秋天","菊花开"),//秋
	winter("冬天","梅花开");//冬
	
	//定义描述信息
	private String description;
	
	//什么花会开
	private String flowerOpen;
	
	//构造方法,将描述信息加入到元素中
	private FourSeasons(String description,String flowerOpen){
		this.description=description;
		this.flowerOpen=flowerOpen;
	}
	
	public String getFlowerOpen() {
		return flowerOpen;
	}

	//获取描述信息
	public String getDescription() {
		return description;
	}

Test category:

public static void main(String[] args) {
		for(FourSeasons forSeasons:FourSeasons.values()) {
			System.out.println("当前的季节是:"+forSeasons.name()+" 下标为:"+forSeasons.ordinal()+",  "
					+forSeasons.getDescription()+forSeasons.getFlowerOpen());
		}
	}

The results are as follows:
Insert picture description here
3. Obtain enumeration elements according to the description information
. Add the following methods to the enumeration class:

/**
	 *  单例方法,根据描述信息获取枚举元素
	 * @param description
	 * @return
	 */
	public static FourSeasons getInstance(String description) {
		for(FourSeasons fourSeasons:FourSeasons.values()) {
			if(description.equals(fourSeasons.getDescription())) {
				return fourSeasons;
			}
		}
		return null;
	}
	

The complete code of the enumeration class is attached at the end of the article:

public enum FourSeasons {
	spring("春天","桃花开"),//春
	summer("夏天","荷花开"),//夏
	autumn("秋天","菊花开"),//秋
	winter("冬天","梅花开");//冬
	
	//定义描述信息
	private String description;
	
	//什么花会开
	private String flowerOpen;
	
	//构造方法,将描述信息加入到元素中
	private FourSeasons(String description,String flowerOpen){
		this.description=description;
		this.flowerOpen=flowerOpen;
	}
	
	public String getFlowerOpen() {
		return flowerOpen;
	}

	//获取描述信息
	public String getDescription() {
		return description;
	}
	
	/**
	 *  单例方法,根据描述信息获取枚举元素
	 * @param description
	 * @return
	 */
	public static FourSeasons getInstance(String description) {
		for(FourSeasons fourSeasons:FourSeasons.values()) {
			if(description.equals(fourSeasons.getDescription())) {
				return fourSeasons;
			}
		}
		return null;
	}
	
	
}


 

Guess you like

Origin blog.csdn.net/liuming690452074/article/details/113928960