2
这是Java 1.6接口:如何返回一个泛型类型而不明确指定其参数?
interface Foo<T extends Foo<T>> {
}
这是一个工厂,它应该返回"Foo<Foo>"
:
interface Factory {
<T extends Foo<T>> Foo<T> find();
}
不能编译工厂类:
incompatible types; inferred type argument(s) java.lang.Object do not conform
to bounds of type variable(s) T
[ERROR] found : <T>Foo<T>
[ERROR] required: java.lang.Object
这里有什么问题?
感谢您的解释,帮助很多。在我的用例中,我希望'Factory.find()'返回'Foo',总是(在问题中提到)。在这种情况下,我应该如何声明'find()'? –
yegor256
2011-04-17 15:00:05
'Foo'不是一个完整的类型,因为'Foo'需要一个类型参数。也许'Foo >'? –
2011-04-17 15:04:14
@Paulo你是对的,'Foo >'工作正常。如果您将它发布为答案,我很乐意将其选为最佳答案。 – yegor256 2011-04-17 15:10:58