Java Scala中具有多个参数的辅助构造函数

Java Scala中具有多个参数的辅助构造函数,java,scala,Java,Scala,我有一个用整数和集合初始化的类。我想创建一个接受整数和多个参数的辅助构造函数。这些多个参数应该是集合的内容 我需要使用合适的参数调用主构造函数的帮助 final class Soccer[A](max: Int, collection_num: Set[A]) { ///Auxiliary Constructor /// new Soccer(n,A,B,C)` is equivalent to `new Soccer(n,Set(A,B,C))`. ///This constructor r

我有一个用整数和集合初始化的类。我想创建一个接受整数和多个参数的辅助构造函数。这些多个参数应该是集合的内容

我需要使用合适的参数调用主构造函数的帮助

final class Soccer[A](max: Int, collection_num: Set[A]) {

///Auxiliary Constructor
/// new Soccer(n,A,B,C)` is equivalent to `new Soccer(n,Set(A,B,C))`.
///This constructor requires at least two candidates in order to avoid ambiguities with the
/// primary constructor.

def this(max: Int, first: A, second: A, other: A*) = {
///I need help calling the primary constructor with suitable arguments.
}

}
new Soccer(n,A,B,C)
应等同于
new Soccer(n,Set(A,B,C))

尝试在同伴对象上定义,而不是像这样定义辅助构造函数

object Soccer {
  def apply[A](max: Int, first: A, second: A, other: A*): Soccer[A] = {
    new Soccer(max, Set[A](first, second) ++ other.toSet)
  }
}
现在像这样建造
Soccer

Soccer(n,A,B,C)

在companion对象中使用apply方法是实现需求的一个好方法,但是如果您真的想在辅助构造函数中使用它,您只需调用
this
方法,如下所示:

final class Soccer[A](最大值:Int,集合数:Set[A]){
定义此(最大值:Int,第一个:A,第二个:A,其他:A*)={
此(最大,设置[A](第一,第二)+其他.toSet)
}
}

为什么有第一个和第二个?只需使用
元素:A*