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

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

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.
}

}

新足球(n,A,B,C)should be equivalent to新足球(n,Set(A,B,C))

最佳答案

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

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)

10-05 23:53