对可能实现该接口的对象调用具有接口约束的泛型方法



这是我的代码:

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

最新更新