operation.java
来自「书籍Effective java的源代码」· Java 代码 · 共 42 行
JAVA
42 行
// Serializable, extensible typesafe enum with attached behaviors - Page 111
import java.io.*;
public abstract class Operation implements Serializable {
private final transient String name;
protected Operation(String name) { this.name = name; }
public static Operation PLUS = new Operation("+") {
protected double eval(double x, double y) { return x+y; }
};
public static Operation MINUS = new Operation("-") {
protected double eval(double x, double y) { return x-y; }
};
public static Operation TIMES = new Operation("*") {
protected double eval(double x, double y) { return x*y; }
};
public static Operation DIVIDE = new Operation("/") {
protected double eval(double x, double y) { return x/y; }
};
// Perform arithmetic operation represented by this constant
protected abstract double eval(double x, double y);
public String toString() { return this.name; }
// Prevent subclasses from overriding Object.equals
public final boolean equals(Object that) {
return super.equals(that);
}
public final int hashCode() {
return super.hashCode();
}
// The 4 declarations below are necessary for serialization
private static int nextOrdinal = 0;
private final int ordinal = nextOrdinal++;
private static final Operation[] VALUES = { PLUS, MINUS, TIMES, DIVIDE };
Object readResolve() throws ObjectStreamException {
return VALUES[ordinal]; // Canonicalize
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?