偏函数
def main(args:Array[String]):Unit={
def funPartition:PartialFunction[String,Int]={
case "hello"=>1
case "world"=>0
case _=>3
}
val i=funPartition("hello")
print(i)
}
程序输出的结果为1
def main(args:Array[String]):Unit={
def funPartition2:PartialFunction[Int,String]={
case 1=>"优秀"
case 2=>"良好"
case 3=>"合格"
case _=>"渣渣"
}
val arr:Array[Int]=Array(1,2,3,4,5)
arr.collect(funPartition2).foreach(println)
}
可以将偏函数当做数组,然后遍历的打印出来,上面代码打印出来的结果是(优秀、良好、合格、渣渣、渣渣)
除了这个遍历方法,还有别的遍历打印的方法,比如:
val arr:Array[Int]=Array(1,2,3,4,5)
val strings:Array[String]=arr.collect(funPartitiion2)
for(str<-strings){
println(str)
}
部分函数
def main(args:Array[String]):Unit={
val title:String="警告"
val content:String="当前河床水位线"
def showAlarm(title:String,content:String,height:Double)={
println(title+" "+content+" "+height)
}
def showMsg=showAlarm(title,_:String,_:Double)
showMsg("当前河床水位线",19.8)
def showMsg1=showAlarm(title,content,_:Double)
showMsg1(23.32)
}
部分函数的作用是当你在代码中需要多次调用一个函数, 而其中的某个参数又总是一样的时候, 使用这个可以使你少敲一些代码
模式匹配
模式匹配是检查某个值(value)是否匹配某一个模式的机制,一个成功的匹配同时会将匹配值解构为其组成部分。它是Java中的switch语句的升级版,同样可以用于替代一系列的 if/else 语句。
def match1(x:Int):String=x match{
case 1=>"one"
case 2=>"two"
case _=>"many"
}
println(match1(3))
上面代码输出的结果为"many"
def match(arr:Array[String]):Unit={
arr match{
case Array("zhangsan")=>println("hello zhangsan")
case Array(str1,str2)=>println(str1,str2)
case Array("zhangsan",_*)=>println("zhangsan",arr(1),arr(2))
case Array(str1,str2,"BBB",_*)=>println(str1,str2,"BBB")
}
}
match(Array("zhangsan","lisi","wangwu"))
上面代码输出的结果为**(zhangsan,lisi,wangwu)**
样例类
class Person
case class Teacher(name:String,age:Int) extends Person
case class Workers(name:String,age:Int) extends Person
def match(person:Person):Unit={
person match{
case Teacher(name,20)=>println(name)
case person:Student if person.name=="小学生"=>println("祖国的花朵")
case Workers(name,age) if name=="repairWorker"=>println("修理工"+age)
case _=>println("不知道是谁")
}
}
伴生类和伴生对象
class Oop(name:String,age:Int){
println("---oop1 class one---")
var uname:String=name
var uage:Int=age
var uaddress:String=""
println("---oop1 class two---")
def this(name:String){
this(name,1)
println("oop1 构造方法")
}
def this(){
this("meiyou",1)
}
def this(name:String,age:Int,address:String){
this(name,age)
uaddress=address
}
def showInfo():Unit={
println("进入class oop1 中的showInfo方法")
println("访问object oop1 中的showInfo方法")
Oop1.showInfo()
println("访问Object oop1 中的showInfo方法结束")
println("name:"+uname=" age:"+uage+" address:"+uaddress+" country:"+Oop1.country)
}
}
object Oop1{
println("---oop1 object one---")
var country:String="中国"
def apply(name:String,age:Int):Oop1=new Oop1(name,age)
def apply():Oop1=new Oop1()
def apply(name:String,age:Int,address:String):Oop1=new Oop1()
def showInfo():Unit={
println("this is oop1 object")
println("---oop1 object three---")
}
println("---oop1 object two---")
def main(arge:Array[String]):Unit={
var oopDemo1=new Oop1("zhangsan",23)
var oopDemo2=new Oop1("lisi")
var oopDemo3=new Oop1("wangwu",6,"上海")
oopDemo3.showInfo()
Oop1.showInfo()
val oopObject=Oop1("zhaoliu",23)
oopObject.showInfo()
val oopObject2=Oop.apply("huangqi",3,"北京")
}
}