One way to use enum-type enums in Java

Source: Internet
Author: User

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

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.