我有以下代碼:Java的泛型類構造函數調用
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()之間的區別?
您打算用仿製藥混合單字母類名來迷惑大家,其通過約定使用單類型的字母。 – artbristol