【发布时间】:2017-01-12 08:53:05
【问题描述】:
我创建了一个名为 Identifier 的界面。实现 Identifier 接口的类是 MasterEntity bean,如下所示。
public class MasterEntity implements Identifier{
}
在DAO方法中,我写了一个类似下面方法签名的方法。
public Identifier getEntity(Class<Identifier> classInstance){
}
现在我想从 Service 调用如下方法。
dao.getEntity(MasterEntity.class);
但是我得到编译错误说 -
The method getEntity(Class<Identifiable>) in the type ServiceAccessObject
is not applicable for the arguments (Class<MasterEntity>)
我知道如果我像下面这样使用它会起作用。
public Identifier getEntity(Class classInstance){
但是通过指定Identifiable类型的Class,如何做到呢?
【问题讨论】:
-
试试
Class<? extends Identifiable>。 -
我相信类型擦除是您第一次尝试失败的原因。但是您能向我们解释一下为什么您认为需要将类类型信息传递到您的方法中吗?
-
你是对的。它的工作。非常感谢。