我有一个预定义的类,叫做
ParameterList<Recipient_Type,Subject_Type> {
//some code here...
}
它是通用的,可以接受收件人类型和主题类型。我有一个名为通信的对象,它具有用于设置不同类型的主题的setter方法,例如
correspondence.setSubject1((Subject1)subject)
correspondence.setSubject2((Subject2)subject).
以及用于设置不同类型的接收者的类似方法
correspondence.setRecipient1((Recipient1)recipient),
correspondence.setRecipient2((Recipient2)recipient),
correspondence.setRecipient3((Recipeint3)recipient).
所以基本上我有2个不同的主题类型和3个不同类型的收件人。直到这部分代码,我不能改变任何东西,因为它是一些现有的框架代码。
现在,我有一个下面的方法,它接受我的通信对象作为参数,需要实例化ParameterList类,传递正确的主题和接收者类型。在通信对象上只有一个主题和接收者设置。因此,在下面的方法中
public void doTheJob(Correspondence correspondence){
//How do I instantiate the ParameterList class provided subject can be any one of the two
//and recipient can be any one of the three.
}
发布于 2011-10-04 04:01:07
您不能在运行时决定泛型类型,因为泛型主要在编译时使用。因此,如果您不知道确切的类型参数,可以使用?
发布于 2011-10-04 04:06:39
您可以执行以下任一操作:
?
,正如Bozho所说,? extends WrapperBean
SubjectN
类继承一个公共的超类型,并且所有的RecipientN
类继承一个公共的超类型https://stackoverflow.com/questions/7639943
复制相似问题