【发布时间】:2012-02-21 00:35:53
【问题描述】:
给定一个随机类:
public class A<T> {
public T t;
public A () {} // <-- why is this constructor necessary for B?
public A (T t) {
this.setT(t);
}
public T getT () {
return this.t;
}
protected void setT (T t) {
this.t = t;
return;
}
}
还有一个扩展类:
public class B extends A<Integer> {
public B (Integer i) {
this.setT(i);
}
}
为什么 B 要求 A 有空的构造函数?我会假设它会想要使用类似的构造函数而不是默认构造函数。我尝试在没有默认构造函数的情况下进行编译,但没有它我收到以下消息...
java.lang.NoSuchMethodError: A: method <init>()V not found at B.<init>
谁能解释这是为什么?
【问题讨论】:
标签: java constructor subclass