-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendedOperation.java
More file actions
57 lines (47 loc) · 1.48 KB
/
ExtendedOperation.java
File metadata and controls
57 lines (47 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package chapter6.interfaces;
/**
* <p>
* 枚举类型{@link BasicOperation}是不可扩展的,而接口类型(Operation)是可扩展的,它是用于在 API 中表示操作的接口类型。
* {@link ExtendedOperation} 是对Operation的又一次扩展
* </p>
* <br>
* <p>
* 现在可以在任何可以使用 {@link Operation} 的地方使用新 Operation,前提是编写的 API 采用接口类型(Operation),而不是实现(BasicOperation)
* 具体可以查看 {@link #main(String[])}
* </p>
*/
public enum ExtendedOperation implements Operation{
EXP("^") {
public double apply(double x, double y) {
return Math.pow(x, y);
}
},
REMAINDER("%") {
public double apply(double x, double y) {
return x % y;
}
},
;
private final String symbol;
public String getSymbol() {
return symbol;
}
@Override
public String toString() {
return symbol;
}
ExtendedOperation(String symbol) {
this.symbol = symbol;
}
// ------------------------------test------------------------------
public static void main(String[] args) {
double x = 2.34;
double y = 2.66;
Operation op = BasicOperation.PLUS;
double res = op.apply(x, y);
System.out.printf("%s %s %s = %s\n", x, op, y, res);
op = ExtendedOperation.EXP;
res = op.apply(x, y);
System.out.printf("%s %s %s = %s\n", x, op, y, res);
}
}