我想知道演员如何将值返回给发送者以及如何将其存储在变量中。
例如,假设我们必须找到 2 个数字的平方和并打印出来。
即总和 = a 2 + b 2
我有2个演员。1 个参与者计算传递给它的任何数字的平方(例如SquareActor
)。另一个参与者将两个数字 (a , b) 发送给SquareActor
并计算它们的总和(例如SumActor
)
/** Actor to find the square of a number */
class SquareActor (x: Int) extends Actor
{
def act()
{
react{
case x : Int => println (x * x)
// how to return the value of x*x to "SumActor" ?
}
}
}
/** Actor to find the sum of squares of a and b */
class SumActor (a: Int, b:Int) extends Actor
{
def act()
{
var a2 = 0
var b2 = 0
val squareActor = new SquareActor (a : Int)
squareActor.start
// call squareActor to get a*a
squareActor ! a
// How to get the value returned by SquareActor and store it in the variable 'a2' ?
// call squareActor to get b*b
squareActor ! b
// How to get the value returned by SquareActor and store it in the variable 'b2' ?
println ("Sum: " + a2+b2)
}
}
如果以上不可能,请原谅我;我想我对演员的基本理解本身可能是错误的。