1. How to customize the enumeration class
①Provide the attributes of the class and declare it as private final
② Properties declared as final are initialized in the constructor
③ Call properties through public methods
④Create an object of the enumeration class: declare the object of the class as public static final
//Enumeration class
class Season{
//1. Provide the properties of the class, declared as private final
private final String seasonName;
private final String seasonDesc;
//2. The properties declared as final are initialized in the constructor
private Season(String seasonName ,String seasonDesc){
this.seasonName=seasonName;
this.seasonDesc=seasonDesc;
}
//3. Call properties through public methods
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
/ /4. Create an object of the enumeration class: declare the object of the class as public static final
public static final Season SPRING = new Season("spring", "春暖花开");
public static final Season SUMMER = new Season("summer", "夏日炎炎");
public static final Season FALL = new Season("fall", "秋高气爽");
public static final Season WINTER = new Season("winter", "白雪皑皑");
@Override
public String toString() {
return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
}
}
1> How to make the enumeration class implement the interface: You can make the objects of different enumeration tired to call the overridden abstract method, which is equivalent to letting each object rewrite the abstract method
//4. Create an object of the enumeration class: put the class's The object is declared as public static final
SPRING("spring", "Spring blossoms"){
public void show(){
System.out.println("Where is spring?");
}
}
2. How to use the enum keyword to define an enumeration class
Method ①:
//public static final Season SPRING = new Season();
SPRING(){
public void show(){
System.out.println("我是春天!");
}
},
//public static final Season SUMMER = new Season();
SUMMER(){
public void show(){
System.out.println("我是夏天!");
}
},
//public static final Season FALL = new Season();
FALL(){
public void show(){
System.out.println("我是秋天!");
}
},
//public static final Season WINTER = new Season();
WINTER(){
public void show(){
System.out.println("I am winter");
}
};
private Season1(){}
Method ②:
public enum Season {
SPRING,SUMMER,FALL,WINTER;
}
3. The method of enum:
values
returns an array
valueOf(String name) containing enum objects;
returns the corresponding enumeration object