Copy Code code as follows:
public class Test {
public static void Main (string[] args) {
Weekday w = Weekday.mon;
System.out.println (w);//oneself will call the ToString method
System.out.println (W.ordinal ());//printing is an enumeration of the first few objects of the list
System.out.println (Weekday.values (). length);//Total number of enumerated objects
}
public enum weekday{
Sat,mon,tue,wed,thu,fri,sat,
Private Weekday () {
System.out.println ("11");
}
Private weekday (int a) {
System.out.println ("a");
}
}
}
First, the above defines a simple enumeration class weekday
The Sat,mon in the class ... And so on is actually the object of the weekday class
Attention:
Enumeration classes also have construction methods, and the construction method must be private;
The following code should be able to understand the use of enumerated types, and to combine the internal classes to understand
Copy Code code as follows:
public enum trefficlamp{
Red (30) {//Red light Object invocation parameter is a construction method of int type,
Public Trefficlamp Nextlamp () {
return GREEN;
}
},
GREEN (20) {
Public Trefficlamp Nextlamp () {
return yellow;
}
},
Yellow (2) {
Public Trefficlamp Nextlamp () {
return RED;
}
};
Public abstract Trefficlamp Nextlamp ();
private int time;
Private Trefficlamp (int time) {
This.time = time;
};
}