The enumeration class is defined as follows:
Package com.qunar.enumtest; Public enumStatus {scuuess ("1","Success"), FAILED ("2","Failed");PrivateStringvalue;PrivateString desc; PublicStringGetValue() {return value; } Public void SetValue(Stringvalue) { This.value=value; } PublicStringGetDesc() {returnDesc } Public void Setdesc(String desc) { This. desc = desc; }Private Status(Stringvalue, String desc) { This.value=value; This. desc = desc; }}
Use the following methods:
Packagecom. Qunar. Enumtest;public class Stutustest {public static void main (string[] args) {System. out. println(Status. Scuuess. GetValue());System. out. println(Status. Scuuess. GetDesc());System. out. println(Status. FAILED. GetValue());System. out. println(Status. FAILED. GetDesc());}}
For example, when we operate the database, we usually use numbers to save to the database, but when the interface is displayed, we need to show their Chinese meaning, south of the No we can be through the following way:
Public enumFlighttype {OW (1,"One Way"), RT (2,"Round trip"); PublicInteger Code; PublicString desc; Flighttype (Integer code, String desc) { This. Code = Code; This. desc = desc; } PublicIntegerGetCode() {returnCode } Public void Setcode(Integer code) { This. Code = Code; } PublicStringGetDesc() {returnDesc } Public void Setdesc(String desc) { This. desc = desc; } Public StaticFlighttypeGettypebycode(Integer code) {Flighttype DefaultType = Flighttype.ow; for(Flighttype ftype:FlightType.values ()) {if(Ftype.code = = code) {returnFtype } }returnDefaultType; } Public StaticStringGetdescbycode(Integer code) {returnGettypebycode (code). Desc; }}
In this way, we can obtain its corresponding Chinese by getdescbycode method.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
One way to use enum-type enums in Java