从枚举返回计算值

时间:2013-04-19 12:35:27

标签: java enums

请问我的问题是两个非常简单

  1. 按错误解释枚举

  2. 这个想法在我的代码中遗漏了一些重要的抽象

  3. 代码示例,oprt.calc(x, y)无法编译,警告cannot find symbol

    public enum Operation {
    
        PLUS {
            public double calc(double x, double y) {
                return x + y;
            }
        },
        MINUS {
            public double calc(double x, double y) {
                return x - y;
            }
        },
        MULTILPLE {
            public double calc(double x, double y) {
                return x * y;
            }
        },
        DIVIDED_BY {
            public double calc(double x, double y) {
                return x / y;
            }
        };
    
        public static void main(String args[]) {
            double x = 15.25;
            double y = 24.50;
            for (Operation oprt : Operation.values()) {
                System.out.println(x + " " + oprt + " " 
                        + y + " = " + oprt.calc(x, y));
            }
        }
    }
    

6 个答案:

答案 0 :(得分:8)

你想念的是calc()方法的抽象声明:

enum Operation {

  PLUS {
      public double calc(double x, double y) {
          return x + y;
      }
  },
  MINUS {
      public double calc(double x, double y) {
          return x - y;
      }
  },
  MULTILPLE {
      public double calc(double x, double y) {
          return x * y;
      }
  },
  DIVIDED_BY {
      public double calc(double x, double y) {
          return x / y;
      }
  };

  **public abstract double calc(double x, double y);**

  public static void main(String args[]) {
      double x = 15.25;
      double y = 24.50;
      for (Operation oprt : Operation.values()) {
          System.out.println(x + " " + oprt + " " 
                  + y + " = " + oprt.calc(x, y));
      }
  }
}

答案 1 :(得分:7)

您需要直接在枚举中声明一个抽象方法double calc(double x, double y),并在每个枚举成员中覆盖它。

答案 2 :(得分:3)

当您没有原始calc()方法时,您将覆盖calc()。要么声明一个抽象方法:

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

或使用默认实现声明具体方法:

public double calc(double x, double y)
{
    // ...
}

答案 3 :(得分:3)

使用枚举方法的正确语法是:

private enum Operation {
    PLUS, MINUS, MULTILPLE, DIVIDED_BY;

    public double calc(double x, double y) {
        switch (this) {
        case PLUS:
            return x + y;
        case MINUS:
            return x - y;
        case MULTILPLE:
            return x * y;
        case DIVIDED_BY:
            return x / y;
        }
        return 0;
    }
}

public static void main(String args[]) throws IOException {

    double x = 15.25;
    double y = 24.50;
    for (Operation oprt : Operation.values()) {
        System.out.println(x + " " + oprt + " " + y + " = "
                + oprt.calc(x, y));
    }

}

答案 4 :(得分:2)

它无法编译,因为目前,calc方法仅存在于enum的每个可能值中 - 但它不存在于类型 { {1}}本身。这就是你的编译器(和我的)不接受它的原因。

因此,您需要在类型中定义方法。也许是这样的:

Operation

您的枚举值(public abstract double calc(double x, double y); PLUSMINUSMULTIPLE均实现了该方法。

答案 5 :(得分:2)

public double calc(double x, double y){}

相同
private double calc(double x,double y){}

除非您将calc()方法添加到枚举Operation。根据JLS:

Instance methods declared in these class bodies are may be invoked outside 
the enclosing enum type only if they override accessible methods in 
the enclosing enum type.

基本上,oprt的类型是Operation,而Operation没有任何名为double calc(double x,double y)的方法的声明,你不能使用{{oprt来调用方法1}}。简而言之,类体中定义的方法应该是重写方法,以便在外部访问它们。