枚举

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

CITY
1
APPLE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值