我创建了一个名为标识符的接口(interface)。实现 Identifier 接口(interface)的类是 MasterEntity bean,如下所示。
public class MasterEntity implements Identifier{
}
在 DAO 方法中,我编写了如下方法签名的方法。
public Identifier getEntity(Class<Identifier> classInstance){
}
现在我想从服务中调用如下方法。
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){
但是通过指定类型为可识别的Class,如何才能做到呢?
最佳答案
将 DAO 方法签名更改为
public Identifier getEntity(Class<? extends Identifier> classInstance)
通过如上所述声明方法,您指定唯一适用的参数是 Identifier.class
本身。能够接受Identifier.class
实现,你应该定义 generic type bounds .
List<? extends Shape>
is an example of a bounded wildcard. The?
stands for an unknown type, just like the wildcards we saw earlier. However, in this case, we know that this unknown type is in fact a subtype of Shape. (Note: It could be Shape itself, or some subclass; it need not literally extend Shape.) We say that Shape is the upper bound of the wildcard.
您也可以看看这个答案:https://stackoverflow.com/a/897973/1782379
关于Java 泛型 - 类<接口(interface)>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41608736/