简体   繁体   English

Java中泛型类型的泛型类型?

[英]Generic Type of a Generic Type in Java?

I have an interface 我有一个界面

public interface BWidgetObject<T> {
}

and I want to use this interface to create a new generic interface based on this type: 我想使用此接口创建基于此类型的新通用接口:

public interface BDataList<BWidgetObject> {}

The former gives a warning that the type T is hidden. 前者发出隐藏类型T的警告。 The following give a compiler error: 以下给出了编译器错误:

public interface BDataList<BWidgetObject<T>> {}

How can I express BWidgetObject<T> as type parameter for BDataList ? 如何将BWidgetObject<T>表示为BDataList类型参数?

You can try with: 您可以尝试:

public interface BDataList<T extends BWidgetObject<?>> {}

Here we're specifying the the type T will be a BWidgetObject of a type we don't actually care about (and that's why we use a wildcard). 这里我们指定类型T将是我们实际上并不关心的类型的BWidgetObject (这就是我们使用通配符的原因)。 We only care about T and the fact it will be a subtype of BWidgetObject . 我们只关心T ,事实上它将是BWidgetObject的子类型。

Use a generic bound : 使用通用绑定

public interface BDataList<T extends BWidgetObject<?>> {}

Or if you need to type the widget explicitly, you need to create another sub-interface: 或者,如果需要显式键入窗口小部件,则需要创建另一个子接口:

public interface BWidgetDataList<T> extends BDataList<BWidgetObject<T>> {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM