Java learning road-enumeration class
Overview
In mathematics and computer science theory, an enumeration of a set is a program that lists all the members of some finite sequence set, or a count of objects of a specific type. These two types often overlap. --Wikipedia
Enumerated type is part of the new features in Java 5, it is a special data type.
Its special feature is that it is a class type but has more special constraints than ordinary class types, but the existence of these constraints also creates the simplicity, safety and convenience of enumeration types.
The enumeration class has the following characteristics:
- The defined
enum
type is always inherited fromjava.lang.Enum
and cannot be inherited; enum
Instances that can only be defined , but cannot benew
created by operatorsenum
;- Each instance defined is the only instance of the reference type;
- You can use
enum
types forswitch
statements.
One, create an enumeration class
In Java programs, using the enumeration class enum
keyword to define, use a comma between the various constants defined , to split.
public class Demo {
public static void main(String[] args) {
System.out.println(Weekday.SUN);
}
}
enum Weekday {
SUN, MON, TUE, WED, THU, FRI, SAT;
}
2. Common operations
1. name()
Method
Returns the string form of the constant name.
System.out.println(Weekday.SUN.name()); // SUN
2. ordinal()
Method
Returns the index value of the constant.
In the enumeration class, each constant has an index value like an array, and the index value is related to the position of the constant. The index value of changing the order of the constant will also change.
System.out.println(Weekday.SUN.ordinal()); // 0
3. valueOf()
Method
Returns the enum constant of the specified string value.
System.out.println(Weekday.valueOf("SUN")); // SUN
4. Traverse
We can experiment for
statement to traverse the enumeration constants class.
for (Weekday var : Weekday.values()) {
System.out.print(var + " ");
} // SUN MON TUE WED THU FRI SAT
5. switch
Use in statements
Scanner scan = new Scanner(System.in);
Weekday day = Weekday.valueOf(scan.nextLine());
switch (day) {
case SUN:
System.out.println("SUN");
break;
case MON:
System.out.println("MON");
break;
default:
System.out.println("Others");
}
Three, advanced use
What type of constants are in the enumeration class?
Each constant in the enumeration class is actually an instance of the enumeration class.
System.out.println(Weekday.SUN.getClass()); // class Weekday
Since every constant is an instance of the enumeration class, the methods and properties defined in the enumeration class should also be applied to every instance.
public class Demo {
public static void main(String[] args) {
System.out.println(Weekday.SUN.getChinese()); // 星期天
}
}
enum Weekday {
SUN("星期天", 7), MON("星期一", 1), TUE("星期二", 2),
WED("星期bai三", 3), THU("星期四", 4), FRI("星期五", 5),
SAT("星期六", 6);
private String chinese;
private int day;
Weekday(String name , int day) {
this.chinese = name;
this.day = day;
}
public String getChinese() {
return chinese;
}
}
It turns out that it is feasible, so we can expand the use of our constants by adding attributes and methods to the enumeration class!