我有一个带有构造函数“A”的抽象类,想用super()的子类的构造函数“B”来使用它。但是,我不得不使用“B”超()在第一行:如果我创建这个“B” -constructor岁用super构造函数初始化()
public class B extends A {
// constructor B
public B(String name){
// cannot have this on the first line
int age = 18;
// constructor "A" of abstract class
super(name, age);
}
}
如何传递的超级构造函数的年龄?
你能更具体吗? –
“如果我在这个”B“ - 构造函数中创建年龄”为什么你没有将它作为参数传递给B()。如果它固定为所有的obj为什么不是静态类字段? – VedX
'super(name,age)'应该是构造函数中的第一条指令 –