enum usage in java

The enumeration class is defined as follows: package java1996;

public enum Status {

SCUUESS("1", "success"), FAILED("2", "failed"); private String value; private String desc; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } private Status(String value, String desc) { this.value = value; this.desc = desc; }

}

The test class is as follows:

package java1996;

public class StatusTest {

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 another example, when we operate the database, we usually use numbers to save to the database, but when we display it on the interface, we need to show its Chinese meaning, then we can use the following methods:

package java1996;

public enum FlightType {

OW(1, "单程"), RT(2, "往返"); public Integer code; public String desc; public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } FlightType(Integer code, String desc) { this.code = code; 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; }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325166590&siteId=291194637