这是我的代码:
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){
}
}
除了可能糟糕的设计之外,我还想知道是否可以在实现接口a
的对象上调用方法test
。
test
方法不需要泛型类型参数。
您可以将其定义为:
public void test(Class<? extends a> classType, a concrete) {
}
附言请使用大写的类名。
您实际上可以在不使用泛型的情况下逃脱:
public void test(a concrete) {
}