java枚举类的作用

java枚举类可以简单地理解为一种特殊的java类,它的主要用途如下:

1、作为类型安全的常量。如下,

public enum Color {
	RED, GREEN, BLANK, YELLOW;
	public static void main(String[] args) {
		System.out.println(Color.RED);//RED
	}
}

之所以打印结果为RED,是因为我们没有重写toString()方法,并且,Color.RED依旧是Color类型。如下验证:

public enum Color {
	RED, GREEN, BLANK, YELLOW;
	@Override
	public String toString() {
		return "color:" + super.toString();
	};
	public static void main(String[] args) {
		System.out.println(Color.RED.BLANK);//color:BLANK
		System.out.println(RED.ordinal());//0
	}
}
上面代码证明两点:1、枚举类中的每个数据都是一个类型;2、枚举类中的每个数据都可以调用枚举类的所有方法;3、枚举类的数据是static同时也是final的。

2、可以实现接口拓展功能

public enum Color implements IColor{
	RED, GREEN, BLANK, YELLOW;
	@Override
	public String toString() {
		return "color:" + super.toString();
	};
	public static void main(String[] args) {
		System.out.println(Color.YELLOW.getColorIndex());//3
	}
	@Override
	public int getColorIndex() {
		return this.ordinal();//ordinal()方法是返回枚举数据的排序下标从0开始,按顺序递增
	}
}

interface IColor {
	int getColorIndex();
}

3、将枚举数据与它的常量关联起来

public enum Season {
	SPRING(20.1),SUMMER(36.5),AUTUMN(12.5),WINTER(0.3);
	/**每个季节的平均气温*/
	private double averageTem;
	/**
	 * 构造方法
	 * @param averageTem
	 */
	private Season(double averageTem) {
		this.averageTem = averageTem;
	}
	/**
	 * 提供一个get方法返回数据
	 * @return
	 */
	public double getAverageTem() {
		return averageTem;
	}
	public static void main(String[] args) {
		System.out.println(Season.valueOf("SPRING").getAverageTem());//20.1
	}
}

4、将枚举数据和它的行为关联起来(特定于常量的方法实现)

public enum Operation {
	ADD {
		@Override
		double apply(double x, double y) {
			return x + y;
		}
	},
	MINUS {
		@Override
		double apply(double x, double y) {
			return x - y;
		}
	},
	TIMES {
		@Override
		double apply(double x, double y) {
			return x * y;
		}
	},
	DEVIDE {
		@Override
		double apply(double x, double y) {
			return x / y;
		}
	};
	/**
	 * 提供一个抽象的方法,强制每个枚举数据都要实现,这样做拓展不易出错
	 * @param x
	 * @param y
	 * @return
	 */
	abstract double apply(double x, double y);
	public static void main(String[] args) {
		System.out.println(Operation.ADD.apply(5, 10));
	}
}

综合例子:

public enum Operation {
	ADD("+") {
		@Override
		double apply(double x, double y) {
			return x + y;
		}
	},
	MINUS("-") {
		@Override
		double apply(double x, double y) {
			return x - y;
		}
	},
	TIMES("*") {
		@Override
		double apply(double x, double y) {
			return x * y;
		}
	},
	DEVIDE("/") {
		@Override
		double apply(double x, double y) {
			return x / y;
		}
	};
	/**
	 * 提供一个抽象的方法,强制每个枚举数据都要实现,这样做拓展不易出错
	 * @param x
	 * @param y
	 * @return
	 */
	abstract double apply(double x, double y);
	/**枚举对应的符号*/
	private String symble;
	private Operation(String symble) {
		this.symble = symble;
	}
	@Override
	public String toString() {
		return this.symble;
	}
	public static void main(String[] args) {
		double x = 5;
		double y = 10;
		for(Operation op:Operation.values()) {
			System.out.printf("%f %s %f = %f%n",x,op,y,op.apply(x, y));
		}
	}
}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值