6

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

  1. 按原样误解枚举

  2. 这个想法在我的代码中缺少一些重要的抽象

代码示例,其中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));
        }
    }
}
4

6 回答 6

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));
      }
  }
}
于 2013-04-19T12:39:59.317 回答
7

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

于 2013-04-19T12:38:13.910 回答
3

你是压倒一切的calc(),而你没有原始calc()方法。要么声明一个抽象方法:

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

或声明一个具有默认实现的具体方法:

public double calc(double x, double y)
{
    // ...
}
于 2013-04-19T12:40:41.937 回答
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));
    }

}
于 2013-04-19T12:45:33.860 回答
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.

所以基本上,oprtisOperation和 as的类型Operation没有任何方法被double calc(double x,double y)调用的声明,你不能调用方法 using oprt。简而言之,类主体中定义的方法应该是被覆盖的方法,以便它们可以在外部访问。

于 2013-04-19T12:44:55.603 回答
2

它不会编译,因为目前,该calc方法仅存在于您的每个可能值中enum- 但它不存在于类型 Operation本身。这就是为什么您的编译器(和我的)不接受它的原因。

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

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

您的枚举值 ( , 和PLUS每个MINUS都实现该方法。MULTIPLEDIVIDED_BY

于 2013-04-19T12:44:00.550 回答