- public enum Operation2
- {
- PLUS
- {
- public double eval(double x , double y)
- {
- return x + y;
- }
- },
- MINUS
- {
- 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)
- {
- return x / y;
- }
- };
- public abstract double eval(double x, double y);//位置1,此处为枚举类定义了一个抽象方法
- public static void main(String[] args)
- {
- System.out.println(Operation.PLUS.eval(3, 4));
- System.out.println(Operation.MINUS.eval(5, 4));
- System.out.println(Operation.TIMES.eval(5, 4));
- System.out.println(Operation.DIVIDE.eval(5, 4));
- }
- }
复制代码 今天读书的时候发现这样一段代码,位置1处的代码好像很多余,是不是去掉也没有区别,不明白此处为何非要定义这么一个抽象方法? |