简体   繁体   中英

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. The following give a compiler error:

public interface BDataList<BWidgetObject<T>> {}

How can I express BWidgetObject<T> as type parameter for 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). We only care about T and the fact it will be a subtype of 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>> {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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