Usage of Enum in Java

1. The Company enumeration class is a class, and it is a final class that cannot be inherited.

Its enumeration values ​​(RED, BLUE...) are all static constants of the Company type. We can get an instance of the Company enumeration class in the following way:
Company c = Company.ZHEJIANG;  

Note: These enumeration values ​​are all public static final, which is the constant way we often define, so it is best for enumeration values ​​in enumeration classes to be all uppercase.

2. Since the enumeration class is a class, of course, there are constructors, methods and data fields in the enumeration type. However, the constructor of the enumeration class is very different:

(1) The constructor is only called when constructing the enumeration value.

ZHEJIANG("Zhejiang", 7),
	HENAN("Henan", 8);
	
	
	private Company(String name, int index){
		this.name = name;
		this.index = index;
	}
	
	public String toString(){
		return this.index + ":" + this.name ;
	}
	
	/**
	 * Get company name
	 * @param index
	 * @return
	 */
	public static String getCompanyName(int index){
		for(Company c: Company.values()){
			if(c.getIndex()==index){
				return c.getName();
			}
		}
		return null;
	}
	
	private String name;
	private int index;

(2) Constructors can only be private, and public constructors are absolutely not allowed. This ensures that external code cannot newly construct instances of the enum class. This also makes perfect sense, since we know that enum values ​​are public static final constants only.

However, the methods and data fields of the enumeration class can allow external access.
Company com = Company.ZHEJIANG;
System.out.println(com.toString());


3. All enumeration classes inherit the methods of Enum. We will introduce these methods in detail below.

(1) ordinal() method: Returns the order of enumeration values ​​in the enumeration type. This order is based on the order in which the enumeration values ​​are declared.

Company.ZHEJIANG.ordinal(); //Return result: 0  
Company.HENAN.ordinal(); //Return result: 1

(2) compareTo() method: Enum implements the java.lang.Comparable interface, so it can compare the order of objects and specified objects. compareTo in Enum returns the difference between the order of two enumeration values. Of course, the premise is that the two enumeration values ​​must belong to the same enumeration class, otherwise a ClassCastException() exception will be thrown. (See the source code for details)
Company.ZHEJIANG.compareTo(Company.ZHEJIANG); //Return result -1

(3) values() method: A static method that returns an array containing all enumeration values.
for(Company c : Company.values()){
			System.out.println(c);
		}


(4) toString() method: Returns the name of the enumeration constant.

(5) valueOf() method: This method corresponds to the toString method and returns the enumeration constant of the specified enumeration type with the specified name.

Company.valueOf("HENAN")

(6) equals() method: Compare the references of two enumeration class objects.

//JDK source code:        
public final boolean equals(Object other) {    
        return this==other;    
}

4. Enumeration classes can be used in switch statements.

Company c = Company.ZHEJIANG;  
		switch(c){  
		        case ZHEJIANG: System.out.println("it's ZHEJIANG");break;  
		        case HENAN: System.out.println("it's HENAN");break;  
		}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326470888&siteId=291194637