Examples of using Java enumerations _java

Source: Internet
Author: User

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;
};
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.