package cn.kgc.cha01.pratice
object Pratice03 {
def main(args: Array[String]): Unit = {
//值守卫
val r1 = "(.*?),(.*?)".r //.r表示支持正则
val r2 = "(.*?),(.*?),(.*?)".r
val str = "tom,is,handsome"
val array:Array[String] = str match {
case r2(a,b,c) =>Array(a,b,c)
case r1(a,b) =>Array(a,b)
}
array.foreach(o=>println(o))
//if守卫
val score = 90;
var level = score match {
case x if(x>=90) => 'A'
case x if(x>=75) => 'B'
case x if(x>=65) => 'C'
case _ => 'D'
}
println(score)
//模式守卫
var a:AnyVal='a'
var b = a match {
case _:Int =>"Int"
case _:Char =>"Char"
case _=>"None"
}
println(b)
}
}
Scala的三种守卫模式
最新推荐文章于 2023-02-22 17:45:12 发布