Examples of using Java enumerations

Source: Internet
Author: User

  We've learned to enumerate when we're learning programming languages, and now we're going to look at the use of enumerations in Java.

The code is as follows: public class Test { public static void main (string[] args) {  Weekday w = Weekday.mon;   System.ou T.println (w);//Will call ToString Method   System.out.println (W.ordinal ());//print is the first few objects   SYSTEM.OUT.PRINTLN of the enumeration list ( Weekday.values (). length)//Total number of enumerated objects &nbsp}  public enum weekday{  Sat,mon,tue,wed,thu,fri,sat,   Private Weekday () {   SYSTEM.OUT.PRINTLN ("one")  }   private weekday (int a) {   system.out . println ("a");  } &nbsp}     First defines a simple enumeration class weekday       Sat,mon in this class. And so on is actually the object of the weekday class   Note:   Enumeration classes also have construction methods, the construction method must be private;   It should be possible to understand the use of enumerated types through the following code, and to incorporate internal classes to understand     code as follows: public enum trefficlamp{  Red (30) {//Red light Object invocation parameter is a construction method of int type,    public trefficlamp Nextlamp () {    R Eturn GREEN;   &NBSP}  },   GREEN {   public trefficlamp Nextlamp () {    return yellow;   &NBSP}  },   yellow (2) {   PUBLIC Trefficlamp Nextlamp () {    return RED;    }  };   Public abstract Trefficlamp nextlamp ();   private int time;   Private Trefficlamp (int time) {   this.time = time;  };  }  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.