一 模式匹配
基础语法
Java
一般匹配类型是整数或字符串
int flag = 1String res = ""swich(flag){case 1 :res = "1";break;case 2 :res = "2";break;default :res = "3";break;}
Scala
val op : Any = ops match {case "+" => a+bcase "-" => a-bcase "*" => a*bcase "/" => a/bcase _ => "没有该操作" //默认}
案例应用
def ops(op:String,a:Int,b:Int): Any = op match {case "+" => a+bcase "-" => a-bcase "*" => a*bcase "/" => a/bcase _ => "没有该操作" //默认}println(ops("++",1,3))
