我在代码中使用了策略模式,这是一个简短的片段:
public interface FindingStrategy<T> {
Callable<T> setAction();
}
class FindSomething implements BindingActionStrategy {
...
@Override
public Callable<SomeObject> setAction() {
return new Callable<SomeObject>() {
@Override
public SomeObject call() throws ... {
return (SomeObject)binding.findSomething(somethingsId);
}
};
}
...
}
我在其他地方做:
Callable<SomeObject> ts = (Callable<SomeObject>) strategy.setAction();
和编译器信号:
unchecked conversion warning
required: Callable<SomeObject>
found: Callable
奇怪的是,如果我这样做,没有警告:
Callable<SomeObject> ts = new Callable<SomeObject>() {
@Override
public SomeObject call() throws ... {
return (SomeObject)binding.findSomething(somethingsId);
}
};
我该如何解决?我试图改变很多,但我仍然收到警告。请帮忙!