0

我在代码中使用了策略模式,这是一个简短的片段:

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);
            }
        };

我该如何解决?我试图改变很多,但我仍然收到警告。请帮忙!

4

2 回答 2

4

确保strategy变量也具有泛型类型信息。

以下将导致此警告:

FindingStrategy strategy;

虽然这应该没问题:

FindingStrategy<SomeObject> strategy;

你甚至不应该需要演员。

于 2012-12-28T13:04:50.680 回答
4

我怀疑你的类定义应该读

class BindingFindingSomething implements FindingStrategy<SomeObject>
于 2012-12-28T13:07:01.103 回答