Enum的使用
public enum Fruit {
APPLE("1"), BANANA("2"), JUICE("3");
// 成员变量
private String type;
// 括号里面是形参
private Fruit(String type) {
// 成员变量,形参
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}public enum TableType {
LINE("line"), SECTION("section"), PROVINCE("province"), CITY("city"), VIP(
"vip"), RELATED_CELL("relatedcell"), NEIGHBOR_CELL("neighborcell"), CELL(
"cell"), GRID_RELATED_KPIS("gridrelatedkpis"), CELL_RELATED_KPIS(
"cellrelatedkpis"), SECTION_RELATED_KPIS("sectionrelatedkpis"), SECTOR_RELATED_KPIS(
"sectorrelatedkpis"), GRID("grid"), TOP_USER("topuser"), GRID_USER(
"griduser"), CELL_USER("celluser"), SECTOR_USER("sectoruser"), POLYGON_USER(
"polygonuser"), SECTOR("sector"), RELATED_SECTOR("relatedsector"), UMTS_ESCNET(
"umtsEscNet"), GSM_ESCNET("gsmEscNet"), DEFAULT("default");
private String type;
private TableType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public static TableType getTableTypeByType(String type) {
for (TableType tType : TableType.values()) {
if (tType.getType().equals(type)) {
return tType;
}
}
return DEFAULT;
}
}
package com.www.enums;
public class Test {
public static void main(String[] args) {
String type = "city";
String lasttype1 = resetTableType(type);
System.out.println(lasttype1);
TableType lasttype2 = TableType.getTableTypeByType(type);
System.out.println(lasttype2);
String fruit = "1";
System.out.println(setFruitType(fruit));
System.out.println(setFruitTypeByType(fruit));
}
private static String resetTableType(String type) {
if (type.equals(TableType.PROVINCE.getType())
|| type.equals(TableType.CITY.getType())) {
return TableType.LINE.getType();
}
return type;
}
private static String setFruitType(String type) {
if (type.equals(Fruit.APPLE)) {
return Fruit.APPLE.getType();
}
return type;
}
public static TableType getTableTypeByType(String type) {
for (TableType tType : TableType.values()) {
if (tType.getType().equals(type)) {
return tType;
}
}
return null;
}
public static Fruit setFruitTypeByType(String type) {
for (Fruit tType : Fruit.values()) {
if (tType.getType().equals(type)) {
return tType;
}
}
return null;
}
}
输出:
line
CITY1
APPLE