Java Enumeration test

Source: Internet
Author: User

Java Enumeration definitions


The enumeration value definition, in the constructor method assignment, overrides ToString ();


Test class

Package com.yjm.test;enum testenum {ye ("Yellow"), Re ("Red"), GR ("green");p rivate string  str;public string getstr ()  {return this.str;} Public void setstr (STRING STR)  {this.str = str;} Testenum (STRING STR) {this.str=str;} @Overridepublic  string tostring ()  {return this.getstr ();};} Enum testenum1 {ye ("Yellow"), Re ("Red"), GR ("green");p rivate string str;public string  getstr ()  {return this.str;} Public void setstr (STRING STR)  {this.str = str;} TESTENUM1 (STRING STR) {this.str=str;} Distinguish the top enum without overriding the ToString method//@Override//public string tostring ()  {//return this.getstr ();/};} Enum testenum2 {ye ("Yellow", 1), Re ("Red", 2), GR ("Green", 3);p rivate string str;private  Int i;public int geti ()  {return i;} Public void seti (Int i)  {this.i = i;} Public sTring getstr ()  {return this.str;} Public void setstr (STRING STR)  {this.str = str;} TestEnum2 (string str,int i) {this.str=str;this.i=i;} Distinguish top enum without overriding toString method//@Override//public string tostring ()  {//return this.getstr () +this.i ;//};} Enum testenum3 {ye ("Yellow", 1), Re ("Red", 2), GR ("Green", 3);p rivate string str;private  Int i;public int geti ()  {return i;} Public void seti (Int i)  {this.i = i;} Public string getstr ()  {return this.str;} Public void setstr (STRING STR)  {this.str = str;} TESTENUM3 (string str,int i) {this.str=str;this.i=i;} Distinguish the top enum without overriding the ToString method @overridepublic string tostring ()  {return this.getstr () +this.i;};} Enum testenum4 {yellow,red,green;} Public class testen{public static void main (String[] args)  { System.out.println ("enum: " +testenum.ye); System.out.println ("enum1: " +testenum1.ye); System.out.println ("enum2: " +testenum2.ye); System.out.println ("enum3: " +testenum3.ye); System.out.println ("enum4: " +testenum4.green);}


Test output results:


Enum:yellow

Enum1:ye

Enum2:ye

Enum3:yellow1

Enum4:green


Enumerations do not override the ToString method to output their own values, and if there are value pairs inside the enumeration values, they need to be constructed in the constructor method.

Java Enumeration test

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.