0

我有一个具有默认配置的简单演员系统。

我有一个类扩展演员

class Test extend Actor {
  def receive: Receive = {
      case Foo(collection) => sender ! extract(collection)
  }

  private def extract(c: List[FooItem]): List[BarItem] = ???
}

这个演员有一个伴生对象

object Test {
  def props: Props = ???
}

像这样进行功能提取是否安全:

object Test {
  def props: Props = ???
  def extract(c: List[FooItem]): List[BarItem] = ???
}

并从另一个演员使用?

4

1 回答 1

1

是的,可以在同伴上定义一个方法,然后在演员类中导入并使用该方法。像这样的东西会很好用:

object Test {
  def props: Props = Props[Test]
  def extract(c: List[FooItem]): List[BarItem] = {
    . . .
  }
}

class Test extend Actor {
  import Test._
  def receive: Receive = {
    case Foo(collection) => sender ! extract(collection)
  }
}
于 2017-04-26T17:44:30.800 回答