我有一个Java程序,我必须在Scala中实现,但我对Scala非常新。阅读了一些SO问题&答案以及阅读一些谷歌检索的案例类资源,我仍然难以掌握如何获取对我收到的消息的引用?示例代码如下:
case class SpecialMessage(key: Int) {
val id: Int = Main.idNum.getAndIncrement().intValue()
def getId(): Int = {
return id
}
}
然后在另一个班级的接收中,我试图用:
来引用这个号码def receive() = {
case SpecialMessage(key) {
val empID = ?? getId() // Get the id stored in the Special Message
// Do stuff with empID
}
}
我无法弄清楚在empID =的正确位置放置什么以获得该ID。这真的很简单,还是通常不会做的事情?
答案 0 :(得分:3)
这两种方法可以做你想做的事情,选择一个最适合的方式
case msg: SpecialMessage => {
val empID = msg.getId() // Get the id stored in the Special Message
// Do stuff with empID
}
case msg @ SpecialMessage(key) => {
val empID = msg.getId() // Get the id stored in the Special Message
// Do stuff with empID
}