Mar*_*ema 6 java enums static interface
我有几个可以通过int找到的枚举.这是通过枚举上的静态方法完成的.例如:
enum Foo {
A, B, C, D, ... ;
public static Foo fromInt(int i) {
switch(i) {
case 15: return A;
case 42: return B;
...
}
}
enum Bar {
BLA, BOO, BEE, ... ;
public static Bar fromInt(int i) {
switch(i) {
case 78: return BLA;
case 22: return BOO;
...
}
}
...
Run Code Online (Sandbox Code Playgroud)
现在在一些代码中我有一个泛型类型T,保证是这些枚举之一,我有一个整数i.如何fromInt通过值调用方法并获取枚举实例i?
我尝试使用静态方法创建一个接口,fromInt并让枚举实现它,但静态方法在接口中不起作用.
我不能使用Java 8.