1、模式匹配 match case
object controllAbstract {
def main(args: Array[String]): Unit = {
val x:Any="123";
x match {
case a:Int=> println("++++");
case b:String=>println("----");
case _ =>print(".....other type");
}
}
}
2、数组匹配
val array = Array(1,2,0,3,4);
array match {
case Array(0)=>print("有0");
case Array(a,b)=>print("含有两个元素");
case Array(0 ,_*)=>print("从0开始");
case _ =>print("other");
}
3、成绩评定采用模式匹配
object controllAbstract {
def main(args: Array[String]): Unit = {
val result = judgeGrade("A");
print(result);
}
def judgeGrade (grade:String): Unit ={
grade match{
case "A" =>print("excellent");
case "B"=>print("Good");
case "C"=>print("Just so so");
case "D"=>print("fail");
}
}
}