斯卡拉 – 误会阿卡变得/不成熟

以下代码

class HotSwapActor extends Actor {
  import context._
  def angry: PartialFunction[Any,Unit] = {
    case "foo" => sender ! "I am already angry!"
    case "bar" => become(happy)
  }

  def happy: PartialFunction[Any,Unit] = {
    case "bar" => sender ! "I am already happy :-)"; unbecome
    case "foo" => become(angry)
  }

  def receive = {
    case "foo" => become(angry)
    case "bar" => become(happy)
  }
}

class OtherActor extends Actor {
  val system = ActorSystem()
  val actor = system.actorOf(Props[HotSwapActor])
  def receive = {
    case "start" =>
      actor ! "foo"
      actor ! "bar"
      actor ! "bar"
      actor ! "foo"
    case a @ _ => println(a)
  }
}

object HotSwapMain extends App {
  val system = ActorSystem()
  val actor = system.actorOf(Props[OtherActor])
  actor ! "start"
}

输出

I am already happy

相关文章

共收录Twitter的14款开源软件,第1页Twitter的Emoji表情 Tw...
Java和Scala中关于==的区别Java:==比较两个变量本身的值,即...
本篇内容主要讲解“Scala怎么使用”,感兴趣的朋友不妨来看看...
这篇文章主要介绍“Scala是一种什么语言”,在日常操作中,相...
这篇文章主要介绍“Scala Trait怎么使用”,在日常操作中,相...
这篇文章主要介绍“Scala类型检查与模式匹配怎么使用”,在日...