使用factory作为功能文字实例化scala类型

时间:2013-01-18 19:35:06

标签: scala

考虑到以前在此处发布的问题的以下答案,我如何更改fac函数以获取构造函数参数?

class BalanceActor[T <: Actor](val fac: () => T) extends Actor {
  val workers: Int = 10

  private lazy val actors = new Array[T](workers)

  override def start() = {
    for (i <- 0 to (workers - 1)) {
      actors(i) = fac() //use the factory method to instantiate a T
      actors(i).start
    }
    super.start()
  }
}

1 个答案:

答案 0 :(得分:5)

对于String参数:

class BalanceActor[T <: Actor](val fac: String => T) extends Actor ...

对于A参数:

class BalanceActor[A, T <: Actor](val fac: A => T) extends Actor ...