interface A {
String n();
}
class B implements A {
@Override
public String n() { return "asdf"; }
}
interface C<T extends A> {
T m(T t);
}
class D implements C<B> {
@Override
public B m(B b) {
return b;
}
}
Class<C<? extends A>> x = D.class;
最后一行有错误
Type mismatch: cannot convert from Class<D> to Class<C<? extends A>>
这对我来说看起来非常好,但也许我错过了类型通配符如何工作的一些微妙之处。有没有办法可以更改最后一行的类型?我需要这个参考,因为我打算稍后再做:
B b = new B();
A y = x.newInstance().m(b);
这也有错误
The method m(capture#1-of ? extends A) in the type C<capture#1-of ? extends A> is not applicable for the arguments (B)
但是,如果我在没有通配符和捕获的情况下使用它,它可以正常工作:
A z = D.class.newInstance().m(b);
不幸的是,我现在有点坚持这一点,任何帮助将不胜感激。
编辑:删除this.
参考
编辑:将 x 更改为
Class<? extends C<? extends A>> x = D.class;
它有效。但是仍然出现错误x.newInstance().m(b)
The method m(capture#2-of ? extends A) in the type Test.C<capture#2-of ? extends A> is not applicable for the arguments (B)