classPerson{ val name = "zhangsan" val age = 18 defsayName() = { "my name is "+ name } }
创建对象
classPerson{ val name = "zhangsan" val age = 18 defsayName() = { "my name is "+ name } }
伴生类和伴生对象
classPerson(xname :String , xage :Int){ var name = Person.name val age = xage var gender = "m" defthis(name:String,age:Int,g:String){ this(name,age) gender = g } defsayName() = { "my name is "+ name }
}
objectPerson{ val name = "zhangsanfeng" defmain(args: Array[String]): Unit = { val person = newPerson("wagnwu",10,"f") println(person.age); println(person.sayName()) println(person.gender) } }
条件判断和循环
If else
/** * if else */ val age =18 if (age < 18 ){ println("no allow") }elseif (18<=age&&age<=20){ println("allow with other") }else{ println("allow self") }
//可以分号隔开,写入多个list赋值的变量,构成多层for循环 //scala中 不能写count++ count-- 只能写count+ var count = 0; for(i <- 1 to 10; j <- 1 until 10){ println("i="+ i +", j="+j) count += 1 } println(count);
//例子: 打印九九乘法表 for (i <- 1 to 9){ for (j <- 1 to i){ print(j + "*" + i + "=" + j * i + "\t") } println() }
4、for循环中可以加条件判断,分号隔开
//可以在for循环中加入条件判断 for(i<- 1 to 10 ;if (i%2) == 0 ;if (i == 4) ){ println(i) }
5、while循环,while(){},do {}while()
//将for中的符合条件的元素通过yield关键字返回成一个集合 val list = for(i <- 1 to 10 ; if(i > 5 )) yield i for( w <- list ){ println(w) }
/** * while 循环 */ var index = 0 while(index < 100 ){ println("第"+index+"次while 循环") index += 1 } index = 0 do{ index +=1 println("第"+index+"次do while 循环") }while(index <100 )
Scala函数
注意点:
函数定义语法 用def来定义
省略return的时候,函数自动回将最后一行的表达式的值,作为返回值
函数的返回类型可以省略,因为scala可以类型自动推断
如果函数有retrun,则必须写返回类型。
函数定义的时候,如果去掉 = ,则相当于,函数就将返回值去掉,既无返回值。
{}里的代码,如果只有一行,则可以省略{}
函数的参数传进来的时候,是一个常量val,不能修改。
Scala函数的定义
有参函数
无参函数
deffun(a: Int , b: Int ) : Unit = { println(a+b) } fun(1,1) deffun1(a : Int , b : Int)= a+b println(fun1(1,2))
递归函数
/** * 递归函数 * 5的阶乘 */ deffun2(num :Int) :Int= { if(num ==1) num else num * fun2(num-1) } print(fun2(5))
val tuple2 = Tuple (1,2) 可以不使用new,也可以直接写成val tuple3 = (1,2,3)
取值用”._XX” 可以获取元组中的值
注意:tuple最多支持22个参数
//创建,最多支持22个 val tuple = newTuple1(1) val tuple2 = Tuple2("zhangsan",2) val tuple3 = Tuple3(1,2,3) val tuple4 = (1,2,3,4) val tuple18 = Tuple18(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18) val tuple22 = newTuple22(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)
//使用 println(tuple2._1 + "\t"+tuple2._2) val t = Tuple2((1,2),("zhangsan","lisi")) println(t._1._2)
3、元组的遍历
tuble.productIterator 得到迭代器,进而遍历
//遍历 val tupleIterator = tuple22.productIterator while(tupleIterator.hasNext){ println(tupleIterator.next()) }
objectLesson_Match{ defmain(args: Array[String]): Unit = { val tuple = Tuple6(1,2,3f,4,"abc",55d) val tupleIterator = tuple.productIterator while(tupleIterator.hasNext){ matchTest(tupleIterator.next()) } } /** * 注意点: * 1.模式匹配不仅可以匹配值,还可以匹配类型 * 2.模式匹配中,如果匹配到对应的类型或值,就不再继续往下匹配 * 3.模式匹配中,都匹配不上时,会匹配到 case _ ,相当于default */ defmatchTest(x:Any) ={ x match { case x:Int=> println("type is Int") case1 => println("result is 1") case2 => println("result is 2") case3=> println("result is 3") case4 => println("result is 4") case x:String => println("type is String") // case x :Double => println("type is Double") case _ => println("no match") } } }
objectLesson_CaseClass{ defmain(args: Array[String]): Unit = { val p1 = newPerson1("zhangsan",10) val p2 = Person1("lisi",20) val p3 = Person1("wangwu",30) val list = List(p1,p2,p3) list.foreach { x => { x match { casePerson1("zhangsan",10) => println("zhangsan") casePerson1("lisi",20) => println("lisi") case _ => println("no match") } } } } }
Actor Model
1、概念理解
Actor Model是用来编写并行计算或分布式系统的高层次抽象(类似java中的Thread)让程序员不必为多线程模式下共享锁而烦恼。
2、Actor的特征
ActorModel是消息传递模型,基本特征就是消息传递。
消息发送是异步的,非阻塞的。
消息一旦发送成功,不能修改。
Actor之间传递时,自己决定决定去检查消息,而不是一直等待,是异步非阻塞的。
3、例:Actor简单例子发送接收消息
import scala.actors.Actor
classmyActorextendsActor{ defact(){ while(true){ receive { case x:String => println("save String ="+ x) case x:Int => println("save Int") case _ => println("save default") } } } }
objectLesson_Actor{ defmain(args: Array[String]): Unit = { //创建actor的消息接收和传递 val actor =new myActor() //启动 actor.start() //发送消息写法 actor ! "I love you !" } }
4、例:Actor与Actor之间通信
caseclassMessage(actor:Actor,msg:Any)
classActor1extendsActor{ defact(){ while(true){ receive{ case msg :Message => { println("I sava msg! = "+ msg.msg) msg.actor!"I love you too !" } case msg :String => println(msg) case _ => println("default msg!") } } } }
classActor2(actor :Actor) extendsActor{ actor ! Message(this,"I love you !") defact(){ while(true){ receive{ case msg :String => { if(msg.equals("I love you too !")){ println(msg) actor! "could we have a date !" } } case _ => println("default msg!") } } } }
objectLesson_Actor2{ defmain(args: Array[String]): Unit = { val actor1 = newActor1() actor1.start() val actor2 = newActor2(actor1) actor2.start() } }