我有一个扩展超类的子类。如果超类中的构造函数具有参数a ,b,c MySuperClass(int a, string b, string c)
。而子类中的构造函数有参数a ,d,eMySubClass(int a, int d, int e)
之类的,子类的构造函数里面应该放什么?我可以说super(a)
我不必复制参数a的代码吗?但是 super 的构造函数有 3 个参数;所以我认为我不能那样做。
另外,如果我只是忽略使用 super 并将字段分配给参数(如this.fieldName=parameterName
),我会得到“super 中没有默认构造函数”的错误,为什么即使超类有构造函数我也会得到这个?
public abstract class Question {
// The maximum mark that a user can get for a right answer to this question.
protected double maxMark;
// The question string for the question.
protected String questionString;
// REQUIRES: maxMark must be >=0
// EFFECTS: constructs a question with given maximum mark and question statement
public Question(double maxMark, String questionString) {
assert (maxMark > 0);
this.maxMark = maxMark;
this.questionString = questionString;
}
}
public class MultiplicationQuestion extends Question{
// constructor
// REQUIRES: maxMark >= 0
// EFFECTS: constructs a multiplication question with the given maximum
// mark and the factors of the multiplication.
public MultiplicationQuestion(double maxMark, int factor1, int factor2){
super(maxMark);
}
}