在Scala中,我有一个必须以下列形式初始化的类:
val box = new Box("foo", "bar", "tut")
这是定义它的类:
class Box(boxMembers: String*) {
val members = boxMembers
}
如果我想扩展基类以具有命名参数,如下所示:
class FlexibleBox(big: String, small: String, otherMembers: String*)
extends Box(big + small + otherMembers) {} //pseudo code
如何将3个参数(大,小和其他familyMembers)传递给它的超级构造函数?
答案 0 :(得分:5)
与m-z的答案类似,但没有转换为List
:
class FlexibleBox(big: String, small: String, otherMembers: String*)
extends Box(big +: small +: otherMembers : _*) {}
答案 1 :(得分:4)
您可以将big
和small
添加到otherMembers
的集合中,然后使用var-args apply语法将它们传递给Box
的构造函数:< / p>
class FlexibleBox(big: String, small: String, otherMembers: String*)
extends Box((big :: small :: otherMembers.toList): _ *)
scala> new FlexibleBox("a", "b", "c", "d", "e")
res0: FlexibleBox = FlexibleBox@478ee483