Return computed value from enumeration

please my question is two and very simple

  • misinterpret enum as is

  • There is no important abstraction in my code in this idea

code example where oprt.calc(x, y) cannot be compiled with a warning 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
source share
6 answers

What you will skip is the abstract declaration of the calc () method:

 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)); } } } 
+8
source

You need to declare the abstract method double calc(double x, double y) in the enumeration directly and override it in each member of the enumeration.

+7
source

You override calc() while you don't have the original calc() method. Or declare an abstract method:

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

or declare a specific method with a default implementation:

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

The correct syntax for using enumeration methods is:

 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)); } } 
+3
source

It does not compile, because currently the calc method exists only in each of the possible values ​​of your enum - but it does not exist in the Operation type itself. This is why your compiler (and mine) does not accept it.

So, you need to define a method in a type. Maybe something like this:

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

Your enumeration values ​​( PLUS , MINUS , MULTIPLE and DIVIDED_BY each implement this method.

+2
source
 public double calc(double x, double y){} 

coincides with

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

unless you add the calc() method to the Operation enumeration. Regarding 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. 

Basically, the type oprt is Operation , and since Operation does not have a declaration for a method called double calc(double x,double y) , you cannot call the method using oprt . In short, methods defined in class classes must be overridden by methods so that they are accessible externally.

+2
source

All Articles