[英]Constructing a generic java class where T is a class foo or a subclasses of foo
I have a class Foo
with subclass SubFoo
, a collection class FooListing
. 我有一个子类SubFoo
Foo
类,一个集合类FooListing
。
I want to create a generic FooListing<T>
where T
is either Foo
or a subclass of Foo
我想创建一个通用FooListing<T>
其中T
要么Foo
或子类Foo
From the wording of the documentation (under 'Wildcards'), it sounds like 从文档的措辞(在“通配符”下),听起来像
public class FooListing<T extends Foo> { ... }
...would let me use FooListing<SubFoo>
, but not FooListing<Foo>
, is this correct? ...让我使用FooListing<SubFoo>
而不是FooListing<Foo>
,这是正确的吗?
No, you're already fine - what you've got allows T
to be Foo
. 不,您已经很好-您所拥有的允许T
成为Foo
。
I've tried to find the relevant bit of the spec, but unfortunately it's extremely opaque :( It definitely works though! 我试图找到规格的相关部分,但不幸的是,它是非常不透明的:(确实可以!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.