枚举不能继承.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 package com.canal.demo;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ZookeeperHello {
public interface Operation{
public long apply(long t1,long t2 );
}

public static enum BasicOperation implements Operation{
ADD(1,"加法") {
public long apply(long t1, long t2) {
return t1+t2;
}
},
MULTIPLY(2,"乘法") {
public long apply(long t1, long t2) {
return t1*t2;
}
};

private int code;
private String desc;
BasicOperation(int code, String desc) {
this.code = code;
this.desc = desc;
}

public String getDesc() {
return desc;
}
}

public static class ComplexOperation implements Operation{
public long apply(long t1, long t2) {
return t1*t1+ t2*t2;
}
}

public static enum NewOperation implements Operation{
MUTLPLYANDADD(2,"先乘法后加法") {
public long apply(long t1, long t2) {
return t1*t2+t2;
}
};

private int code;
private String desc;
NewOperation(int code, String desc) {
this.code = code;
this.desc = desc;
}

public String getDesc() {
return desc;
}
}

public static void main(String[] args) {
List<Operation> operationList=new ArrayList();
operationList.add(new ComplexOperation());
operationList.addAll(Lists.newArrayList(BasicOperation.values()));
operationList.addAll(Lists.newArrayList(NewOperation.values()));
operations(operationList,2L,3L);
}

//使用通配符 进行类型限制
private static void operations(Collection<? extends Operation> operations, long op1, long op2){
for(Operation operation: operations){
System.out.println("result is "+ operation.apply(op1,op2));
}
}

}