如何创建通用< T>类,以便T扩展Collections< String&gt ;? [英] How to create a generic <T> class so that T extends Collections<String>?

查看:71
本文介绍了如何创建通用< T>类,以便T扩展Collections< String&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个泛型ThreadWorker类,它的泛型类型<T>接受扩展了<String>类型的Collection类的类.

I want to create a generic ThreadWorker class that it's generic type <T> accepts classes that extends Collection class of type <String>.

我尝试过:

public class Worker <T extends Collections<String>> extends SwingWorker<VectorDetails , String>    

但是我得到一个错误:

The type Collections is not generic; it cannot be parameterized with arguments <String>

也是我写的时候:

public class Worker <T extends Collections> extends SwingWorker<VectorDetails , String>  

它可以编译,但是那不是我所需要的,因为可以使用不是String的其他类的类型来创建此Worker类的实例. (类似于< Integer><Character>等.).

It compiles fine, but thats not what I need since instances of this Worker class can be created with types of other classes which are not String. (Like < Integer> , <Character> , etc..).

如何强制用户使用扩展/实现包含字符串的Collection类的类型来创建我的类的实例?

How can I force the user to create instances of my class with types that extends/implements Collection class that holds Strings?

推荐答案

您正在寻找什么吗?

class StringCollection<T extends Collection<H>, H extends String> {}

这篇关于如何创建通用&lt; T&gt;类,以便T扩展Collections&lt; String&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆