Java中枚举类型Enum的一种使用方式

枚举类定义如下:

package com.qunar.enumtest;public enum Status {SCUUESS(“1”, “成功”), FAILED(“2”, “失败”);private String value;private String desc;public String getValue() {return value;}(String value) {this.value = value;}public String getDesc() {return desc;}(String desc) {this.desc = desc;}private Status(String value, String desc) {this.value = value;this.desc = desc;}}

使用的方式如下:

package public class StutusTest {public static void main(String[] args) {System());System());System());System());}}

再比如,我们在操作数据库的时候,通常使用数字保存到数据库中,但是在界面上显示的时候,需要展示其中文意思,南无我们就可以通过下边的方式:

public enum FlightType {OW(1, “单程”), RT(2, “往返”);public Integer code;public String desc;FlightType(Integer code, String desc) {this.code = code;this.desc = desc;}public Integer getCode() {return code;}(Integer code) {this.code = code;}public String getDesc() {return desc;}(String desc) {this.desc = desc;}public static FlightType getTypeByCode(Integer code) {FlightType defaultType = FlightType.OW;for (FlightType ftype : FlightType.values()) {if (ftype.code == code) {return ftype;}}return defaultType;}public static String getDescByCode(Integer code) {return getTypeByCode(code).desc;}}

这样的话,我们就可以通过getDescByCode方法获得其对应的中文。

,正确的寒暄必须在短短一句话中明显地表露出你对他的关怀。

Java中枚举类型Enum的一种使用方式

相关文章:

你感兴趣的文章:

标签云: