我想知道是否有可能在Scala中继承辅助构造函数?是否有可能在Scala中继承辅助构造函数?
我测试此代码,它抱怨
temp.scala:18: error: too many arguments for constructor Child:()this.Child
val a = new Child(42)
^
abstract class Father {
var value: Int = 0
protected def setValue(v: Int) = {
value = v
}
protected def this(v: Int) = {
this()
setValue(v)
}
}
class Child extends Father {
}
val a = new Child(42)
但是,如果我在子类中把
protected def this(v: Int) = {
this()
setValue(v)
}
,万事大吉。
在你提供的,“否”的证据。 – 2010-09-25 12:32:24
相关问题(和答案):http://stackoverflow.com/questions/1095329/scala-constructor-overload/1096534#1096534 – 2010-09-25 12:38:12
构造函数没有被继承。不在Scala中,不在Java中。不是主要的,不是次要的。 – 2010-09-25 14:28:27