这是我的代码:
interface a {}
class b{}
class c extends b implements a{}
class d extends b{}
class e{
public void makeItWork(){
b[] bees = new b[] {new c(), new d()};
for (b bee: bees){
if (bee instanceof a) {
a beeA = (a) bee;
//how to call the method test if object bee conforms the the interface?
test(beeA.getClass(), beeA);
//this goes wrong
}
}
}
public <T extends a> void test(Class<T> classType, T concrete){
}
}
除了可能是糟糕的设计之外,我想知道是否可以在实现接口(interface)a
的对象上调用方法test
。
最佳答案
您的test
方法不需要泛型类型参数。
您可以将其定义为:
public void test(Class<? extends a> classType, a concrete) {
}
附注请使用大写的类名。
关于java - 调用对可能实现该接口(interface)的对象进行接口(interface)约束的泛型方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49382457/