Java 对可能实现该接口的对象调用带有接口约束的泛型方法
这是我的代码:Java 对可能实现该接口的对象调用带有接口约束的泛型方法,java,generics,Java,Generics,这是我的代码: 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) {
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{}
b类{}
类c扩展了b实现了{}
类d扩展了b{}
e类{
公共空间使其工作(){
b[]bees=new b[]{new c(),new d()};
(b蜜蜂:蜜蜂){
如果(a的蜜蜂实例){
蜜蜂=蜜蜂;
//如果对象符合接口,如何调用方法测试?
测试(beeA.getClass(),beeA);
//这出了问题
}
}
}
公共空隙试验(T类混凝土){
}
}
除了糟糕的设计之外,我想知道是否可以对实现接口的对象调用方法
test
,a
您的test
方法不需要泛型类型参数
您可以将其定义为:
public void test(Class<? extends a> classType, a concrete) {
}
public void test(Class您实际上可以在这里不使用泛型:
public void test(a concrete) {
}