如何从 Actor 类继承受保护的函数



我有一个扩展演员的类foo

class foo extends Actor{
  def receive:PartialFunction[Any, Unit] = ???
  protected def fooFunctionIWant = { print("hello")}
}

我还有另一个类"bar",我想扩展foo,这样它就会继承fooFunctionIWant。

但是当我尝试时:

object bar extends foo {
  def barFunc = {
     fooFunctionIWant
  }
}

然后:

bar.barFunc

我得到 :

Caused by: akka.actor.ActorInitializationException: You cannot create an instance of [...] explicitly using the constructor (new). You have to use one of the 'actorOf' factory methods to create a new actor. See the documentation.
    at akka.actor.ActorInitializationException$.apply(Actor.scala:173) ~[akka-actor_2.11-2.4.1.jar:na]
    at akka.actor.Actor$class.$init$(Actor.scala:436) ~[akka-actor_2.11-2.4.1.jar:na]

创建执行组件实例的唯一正确方法是使用actorOf方法之一。使用 new Actorobject Actor 将在运行时失败。

你不能直接调用Actor中的任何方法,你只能向它发送消息并从中接收消息。否则,如果可能的话,它将破坏Actor封装。

如果要在 actor 和常规类之间共享代码,可以将该共享代码放在 trait 中并从中继承。但是,您的 actor 只能在内部使用该代码,并且不会公开任何方法,其从 actorOf 返回的实例类型仍将仅Actor

最新更新