将Seq [String]传递给超类

时间:2016-04-21 14:51:53

标签: scala

在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)传递给它的超级构造函数?

2 个答案:

答案 0 :(得分:5)

与m-z的答案类似,但没有转换为List

class FlexibleBox(big: String, small: String, otherMembers: String*)
    extends Box(big +: small +: otherMembers : _*) {}

答案 1 :(得分:4)

您可以将bigsmall添加到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