java_包含抽象方法的枚举类

package ming;



enum Operation {

	PLUS {



		public double eval(double x, double y) {

			return x + y;

		}



	},

	MINS {



		public double eval(double x, double y) {

			return x - y;

		}



	},

	TIMES {



		public double eval(double x, double y) {

			return x * y;

		}



	},

	DIVIDE {

		public double eval(double x, double y) {

			if (y == 0) {

				return -1;

			}

			return x / y;

		}

	};

	//为枚举类定义抽象方法,具体由枚举值提供实现

	public abstract double eval(double x, double y);



}



public class OperationTest {



	public static void main(String[] args) {

		// TODO Auto-generated method stub

		System.out.println(Operation.PLUS.eval(1, 2));

		System.out.println(Operation.DIVIDE.eval(1, 0));



	}



}


你可能感兴趣的:(java)