public enum Operation {
PLUS {
double eval(double x, double y) { return x + y; }
},
MINUS {
double eval(double x, double y) { return x - y; }
},
TIMES {
double eval(double x, double y) { return x * y; }
},
DIVIDED_BY {
double eval(double x, double y) { return x / y; }
};
// Perform the arithmetic operation represented by this constant
abstract double eval(double x, double y);
public static void main(String args[]) {
double x = 2.0;
double y = 4.0;
for (Operation op : Operation.values())
System.out.println(x + " " + op + " " + y + " = " + op.eval(x, y));
}
}
The document says it is anticipated that the need for this will be rare and it it a bit tricky.
How people think it is tricky?
Reference
Tuesday, August 10, 2010
"Operation" Enum
Labels:
Java
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment