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