Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对可能实现该接口的对象调用带有接口约束的泛型方法_Java_Generics - Fatal编程技术网

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) {
}