简体   繁体   English

构造一个通用的Java类,其中T是foo类或foo的子类

[英]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.

相关问题 void foo(T y)和 <T> Java泛型类中的void foo(T y) - Difference between void foo(T y) and <T> void foo(T y) in Java generic class 上课 <Foo<T> &gt;在运行时给出通用类型输入 - Get a Class<Foo<T>> at runtime given generic type inputs 如何实例化类的泛型变量 <Foo<T> &gt;? - How to instantiate generic variable like Class<Foo<T>>? 什么是清单类别 <Foo> 在Java中? - What is the Class Of List<Foo> in Java? Java - 如何子类化通用ArrayList,以便MyArrayList <foo>的实例成为ArrayList <foo>的子类? - Java - How to subclass the generic ArrayList so that instances of MyArrayList<foo> will be subclasses of ArrayList<foo>? java 是否支持 Foo 类<T super X> ,如果没有,为什么? - Does java support class Foo<T super X> ,if no,why? Java:当foo()在超类中时,this.foo()和super.foo()是否相同? - Java: Are this.foo() and super.foo() the same when foo() is in the super class? foo.class java文件中的指令数 - number of instructions in foo.class java file 是否可以做“上课” <? extends foo> foo”? - Is it possible to do “Class<? extends foo> foo”? Java-对于给定的函数foo(X i); 在接口X中,为什么不能实现类Y将其更改为foo(Y i)? - Java - For a given function foo(X i); in Interface X, why can't implementing class Y change it to foo(Y i)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM