码迷,mamicode.com
首页 > 其他好文 > 详细

MyActor1

时间:2018-07-07 22:35:05      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:equal   less   cto   case   rgs   object   com   could   receive   

package com.bjsxt.scala

import scala.actors.Actor
case class Message(actor:Actor,msg:String)

class MyActor1 extends Actor {
  def act(): Unit = {
    while(true){
    	receive {
      	case m:Message=>{
      		if(m.msg.equals("hello~")){
      			println("hello~")
      			m.actor ! "hi~"
      		}else if(m.msg.equals("could we have a dinner?")){
      			println("could we have a dinner?")
      			m.actor ! "no..."
      		}
      	}
      	case _=>{
      		println("no match...")
      	}
    	}
    }
  }
}
class MyActor2(actor:Actor) extends Actor {
  actor ! Message(this,"hello~")
	def act(): Unit = {
    while (true) {
      receive {
        case s: String => {
          if (s.equals("hi~")) {
            println("hi~")
            actor ! Message(this, "could we have a dinner?")
          } else if (s.equals("no...")) {
            println("no...")
            println("。。。。")
          }
        }
        case _ => {
          println("no match...")
        }
      }
    }
	}
}
object Lesson_Actor2 {
  def main(args: Array[String]): Unit = {
    val actor1 = new MyActor1()
		val actor2 = new MyActor2(actor1)
    actor1.start()
    actor2.start()
    
  }
}

  

MyActor1

标签:equal   less   cto   case   rgs   object   com   could   receive   

原文地址:https://www.cnblogs.com/huiandong/p/9278295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!