我有以下代码:
我有以下代码:
public class A {}
public class B extends A {}
public class C <T extends A> {
private final T data;
public C(final T data) {
this.data = data;
}
}
public class D<T extends B> extends C<T> {
public D(T data) {
super(data);
}
public D() {
this(new B());
}
public static D<B> create() {
return new D(new B());
}
}
类D中存在编译错误:
error: no suitable constructor found for D(B)
this(new B());
constructor D.D() is not applicable
(actual and formal argument lists differ in length)
constructor D.D(T) is not applicable
(actual argument B cannot be converted to T by method invocation conversion)
where T is a type-variable:
T extends B declared in class D
让我困惑的是,静态方法D.create()基本上也做了同样的事情,但却编译没有任何错误。有人能解释这个错误吗?以及D()和D.create()之间的区别是什么?