我一直在努力处理与case语句中密封特征对象使用相关的以下错误。这是我与akka actors相关的代码。我正在使用伴随对象并定义密封特征,如下面的scala文件所示。< / p>
ActorCreation.scala
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
object MusicController{
sealed trait ControllerMsg
case object Play extends ControllerMsg
case object Stop extends ControllerMsg
}
class MusicController extends Actor{
def receive ={
case Play =>
println("Music Started.....")
case Stop =>
println("Music Stopped.....")
}
}
object MusicPlayer{
sealed trait PlayMsg
case object StopMusic extends PlayMsg
case object StartMusic extends PlayMsg
}
class MusicPlayer extends Actor {
def receive ={
case StopMusic =>
println("I don't Stop Music now.....")
case StartMusic =>
val controller = context.actorOf(Props[MusicController],"controller")
controller ! Play
case _ =>
println("Unknown Message")
}
}
object Creation extends App{
val system = ActorSystem("creation")
val player = system.actorOf(Props[MusicPlayer],"player")
player ! StartMusic
}
这是我在与各个类的伴随对象中定义密封特征对象时与密封特征对象相关的错误堆栈。
[info] Updating {file:/C:/Akka%20Actors/chapter02/}chapter02...
[info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to C:\Akka Actors\chapter02\target\scala-2.11\classes...
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:14: not found: value Play
[error] case Play =>
[error] ^
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:16: not found: value Stop
[error] case Stop =>
[error] ^
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:30: not found: value StopMusic
[error] case StopMusic =>
[error] ^
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:32: not found: value StartMusic
[error] case StartMusic =>
[error] ^
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:34: not found: value Play
[error] controller ! Play
[error] ^
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:46: not found: value StartMusic
[error] player ! StartMusic
[error] ^
[error] 6 errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 10 s, completed Sep 22, 2017 11:12:37 AM
>
有关您的信息,请访问 build.sbt
name := "akka-actors"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies +=
"com.typesafe.akka" % "akka-actor_2.11" % "2.4.0"
答案 0 :(得分:2)
你只需要将伴侣对象中的那些对象导入到actor中,这是一个使用MusicController的例子:
object MusicController {
sealed trait ControllerMsg
case object Play extends ControllerMsg
case object Stop extends ControllerMsg
}
class MusicController extends Actor{
import MusicController._ // <--- Add this
def receive = {
case Play =>
println("Music Started.....")
case Stop =>
println("Music Stopped.....")
}
}
或者,您可以将import语句添加到文件的顶部,以便所有内容都可以访问它们:
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import example.MusicPlayer._ // new
import example.MusicController._ // new