Scala基本语法(二)

时间:2022-05-15 14:53:52

Point 1:Actor

package com.scala.learn

import scala.actors._, Actor._

object Actor1 {
def main(args: Array[String]) = {
val badActor =
actor {
while (true) { //nexttpule
receive {
case msg =>
println(msg);
reply("gagagagag");
}

}
}

println("Do ya feel lucky, punk?");
// println(badActor ! "dddddddddddddd"); //! 异步发送消息
//这里的println打印的是返回来的值,也就是reply中的值
println(badActor ! "mmmmmmmmmmmmmmm");

}
}

Scala基本语法(二)

Point 2:

package com.scala.learn

import scala.actors._, Actor._
object Actor2 {
case class Speak(line: String)
case class Gesture(bodyPart: String, action: String)
case class NegotiateNewContract()

def main(args: Array[String]) =
{
val badActor =
actor {
receive {
case NegotiateNewContract =>
println("I won't do it for less than $1 million!")
case Speak(line) =>
println(line)
case Gesture(bodyPart, action) =>
println("(" + action + "s " + bodyPart + ")")
case _ =>
println("Huh? I'll be in my trailer.")
}
}

// badActor ! NegotiateNewContract
// badActor ! Speak("Do ya feel lucky, punk?")
// badActor ! Gesture("face", "grimaces")
// badActor ! Speak("Well, do ya?")
badActor ! "Well, do ya?"
Thread.sleep(3000);
}
}

Scala基本语法(二)

Point 3:

package com.scala.learn

import scala.actors._, Actor._
object Actor3 {

case class Speak(line : String);
case class Gesture(bodyPart : String, action : String);
case class NegotiateNewContract();
case class ThatsAWrap();

def main(args : Array[String]) =
{
val badActor =
actor
{
var done = false
while (! done) //相当于true
{
receive
{
case NegotiateNewContract =>
System.out.println("I won't do it for less than $1 million!")
case Speak(line) =>
System.out.println(line)
case Gesture(bodyPart, action) =>
System.out.println("(" + action + "s " + bodyPart + ")")
case ThatsAWrap =>
System.out.println("Great cast party, everybody! See ya!")
done = true
case _ =>
System.out.println("Huh? I'll be in my trailer.")
}
}
}

badActor ! NegotiateNewContract
// badActor ! Speak("Do ya feel lucky, punk?")
// badActor ! Gesture("face", "grimaces")
// badActor ! Speak("Well, do ya?")
// badActor ! ThatsAWrap
}
}

Scala基本语法(二)

Point 4:

package com.scala.learn

import scala.actors._, Actor._
object Actor4 {

case class Speak(line : String);
case class Gesture(bodyPart : String, action : String);
case class NegotiateNewContract();
case class ThatsAWrap();

def main(args : Array[String]) =
{
def ct =
"Thread " + Thread.currentThread().getName() + ": "
val badActor =
actor
{
var done = false
while (! done)
{
receive
{
case NegotiateNewContract =>
System.out.println(ct + "I won't do it for less than $1 million!")
case Speak(line) =>
System.out.println(ct + line)
case Gesture(bodyPart, action) =>
System.out.println(ct + "(" + action + "s " + bodyPart + ")")
case ThatsAWrap =>
System.out.println(ct + "Great cast party, everybody! See ya!")
done = true
case _ =>
System.out.println(ct + "Huh? I'll be in my trailer.")
}
}
}

System.out.println(ct + "Negotiating...")
badActor ! NegotiateNewContract
System.out.println(ct + "Speaking...")
badActor ! Speak("Do ya feel lucky, punk?")
System.out.println(ct + "Gesturing...")
badActor ! Gesture("face", "grimaces")
System.out.println(ct + "Speaking again...")
badActor ! Speak("Well, do ya?")
System.out.println(ct + "Wrapping up")
badActor ! ThatsAWrap
}
}

Scala基本语法(二)

Point 5:

package com.scala.learn

import scala.actors.Actor

/*
给Actor发消息
*/

class HelloActor extends Actor{
def act(){
while (true){
receive{
case name:String => println("Hello, " + name)
case money:Int => println("How much? " + money)
case _ => "other"
}
}
}
}

object HelloActor{
def main(args: Array[String]) {
val helloActor = new HelloActor
helloActor.start()
helloActor ! 100
}
}

Scala基本语法(二)

Point 6:

package com.scala.learn

import scala.actors.Actor

/*
给Actor发送case class的消息
*/

case class Register(username: String, password:String)
case class Login(username: String , password:String)

class UserManagerActor extends Actor{
def act{
while (true){
receive{
case Login(username, password) => println("login: " + username + ": " + password)
case Register(username, password) => println("register: " + username + ": " + password)
}
}
}
}

object UserManagerActor{
def main(args: Array[String]) {
val userActor = new UserManagerActor
userActor.start()
userActor ! Register("Aaron", "123")
userActor ! Login("Aaron", "123")
}
}

Scala基本语法(二)

Point 7:

package com.scala.learn

import scala.actors.Actor
/*
Actor之间相互收发消息
*/

case class Message(content:String, sender:Actor)

class YasakaActor extends Actor{
def act(){
while (true){
receive{
case Message(content,sender) => {println("Aaron received: " + content);
sender ! "Yes"}
}
}
}
}

class GagaActor(val yasakaActor: YasakaActor) extends Actor{
def act{
yasakaActor ! Message("Hello, Aaron, I'm gaga. Are you free now?", this)

var flag = true
while (flag){
receive{
case response:String => println("gaga received: "+ response)
case response:Int => println("you love xuruyun. you don't love me anymore !"); flag=false
}
}
}
}

object Message {
def main(args: Array[String]) {
val yasakaActor = new YasakaActor
val gagaActor = new GagaActor(yasakaActor)
yasakaActor.start()
gagaActor.start()

}
}

Scala基本语法(二)