Scala学习——模式匹配
scala模式匹配
1.基础match case(类似java里switch case,但功能强大些)
object MatchApp { def main(args: Array[String]): Unit = { val is = Array("a","b","c","d") val i = is(Random.nextInt(is.length))//随机取数组中的一个值 i match { case "a" => println("aaa") case "b" => println("bbb") case "c" => println("ccc") case _ => println("没有此匹配") } }
2、带条件的模式匹配
package top.ruandb.scala.Course05 import scala.util.Random object MatchApp { def main(args: Array[String]): Unit = { judgeGrade("jack","A")//非常好 judgeGrade("lucy","B")//好 judgeGrade("lucy","D")//lucy同学,该努力学习了 judgeGrade("jack","D")//同学,该努力学习了 } def judgeGrade(name:String ,grade:String): Unit ={ grade match{ case "A" => println("非常好") case "B" => println("好") case "C" => println("一般") case _ if(name == "lucy") => println(name+"同学,该努力学习了")//带条件筛选 case _ => println("同学,该努力学习了") } } }
3、Array模式匹配
package top.ruandb.scala.Course05 import scala.util.Random object MatchApp { def main(args: Array[String]): Unit = { greeting(Array("a"))//hi,a greeting(Array("b","c" ,"d"))//hi.everyone greeting(Array("a","b","c" ,"d"))//hi,a and others greeting(Array("c" ,"d"))//hi,c d } def greeting(array:Array[String]): Unit ={ array match { case Array("a") => println("hi,a")//匹配数组Array("a") case Array(x,y) => println("hi,"+ x + " "+ y)//匹配两个元素的数组 case Array("a",_*) => println("hi,a and others")//匹配以a开头的数组 case _ => println("hi.everyone") } } }
4、List模式匹配
package top.ruandb.scala.Course05 import scala.util.Random object MatchApp { def main(args: Array[String]): Unit = { greeting(List("a"))//hi,a greeting(List("b","c" ,"d"))//hi.everyone greeting(List("a","b","c" ,"d"))//hi,a and others greeting(List("c" ,"d"))//hi,c d } def greeting(list:List[String]): Unit ={ list match { case "a"::Nil => println("hi,a")//匹配只含a的List case x::y::Nil => println("hi,"+ x + " "+ y)//匹配两个元素的List case "a"::tail => println("hi,a and others")//匹配以a开头的List case _ => println("hi.everyone") } } }
5、类型匹配
package top.ruandb.scala.Course05 import scala.util.Random object MatchApp { def main(args: Array[String]): Unit = { matchType(1)//Int: 1 matchType("str")//String; str matchType(Map("lucy"->18,"jack"->19,"kangkang"->99))//(lucy,18)(jack,19)(kangkang,99) matchType(3.14)//其他类型 } def matchType(obj:Any): Unit ={ obj match{ case x:Int =>println("Int: "+ x ) case s:String =>println("String; "+s) case m:Map[_,_] => m.foreach(println) case _ => println("其他类型") } } }
6、异常处理
package top.ruandb.scala.Course05 object ExceptionApp { def main(args: Array[String]): Unit = { try{ val i = 100/0 //ArithmeticException println(i) }catch { case e:ArithmeticException => println("分母不能为0") case e:Exception => println(e.getMessage) }finally { println("用于释放资源,一定能执行") } } }
7、class模式匹配
package top.ruandb.scala.Course05 import scala.util.Random object MatchApp { def main(args: Array[String]): Unit = { caseClassMatch(Cto("lucy","99")) caseClassMatch(Employee("jack","7")) caseClassMatch(Others("kangkang")) } def caseClassMatch(person:Person): Unit ={ person match{ case Cto(name,floor) => println("hi,"+name +" "+ floor) case Employee(name,floor) =>println("hi,"+name +" "+ floor) case Others(name) => println("hi,"+name ) } } class Person case class Cto(name:String,floor:String) extends Person case class Employee(name:String,floor:String) extends Person case class Others(name:String) extends Person }
8、Some,None模式匹配
package top.ruandb.scala.Course05 import scala.util.Random object MatchApp { def main(args: Array[String]): Unit = { getGrade("lucy")//your grade is A getGrade("jack")//your grade is B getGrade("kangkang")//without your score } val grades = Map("lucy"->"A","jack"->"B") def getGrade(name:String): Unit ={ val grade = grades.get(name) grade match{ case Some(grade) => println("your grade is "+ grade) case None =>println("without your score") } } }