1

鉴于以下对先前在此处发布的问题的回答,我将如何更改 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()
  }
}
4

1 回答 1

5

对于一个String论点:

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

对于一个A论点:

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

于 2013-01-18T19:40:29.823 回答