sco*_*rpp 4 constructor scala super constructor-overloading
我无法理解如何在Java上开发类似于以下内容的Scala代码:
public abstract class A {
protected A() { ... }
protected A(int a) { ... }
}
public abstract class B {
protected B() { super(); }
protected B(int a) { super(a); }
}
public class C extends B {
public C() { super(3); }
}
Run Code Online (Sandbox Code Playgroud)
虽然很清楚如何开发C类,但我无法获得如何接受B.帮助.
PS我正在尝试创建自己的基于wicket WebPage的BaseWebPage,这是Java的常见做法
你的意思是:
abstract class A protected (val slot: Int) {
protected def this() = this(0)
}
abstract class B protected (value: Int) extends A(value) {
protected def this() = this(0)
}
class C extends B(3) {
}
Run Code Online (Sandbox Code Playgroud)
有,AFAIK,无法绕过其中一个辅助表单的主要构造函数,即,以下将不起作用:
abstract class B protected (value: Int) extends A(value) {
protected def this() = super()
}
Run Code Online (Sandbox Code Playgroud)
所有辅助构造函数表单必须调用主要表单.从语言规范(5.3.1构造函数定义):
除了主构造函数之外,类可能还有其他构造函数.这些是由def(this)(ps1)...(psn)= e形式的构造函数定义定义的.这样的定义为封闭类引入了一个额外的构造函数,其参数在形式参数列表ps1,...,psn中给出,其评估由构造函数表达式e定义.每个形式参数的范围是后续参数部分和构造函数表达式e.构造函数表达式是自构造函数调用this(args1)...(argsn)或以自构造函数调用开头的块
(强调我的).