模式匹配
case class Pig(name:String)val pig = Pig("pig")pig match { //需要case class,普通class不可以 //根据传入构造器的参数做精准匹配 case Pig("pig1") => { println("pig1") } case Pig("pig2") => { println("pig2") } case x:Pig => { println("pig") } case _ => { }}class Match { def test(x:Int) = x match { case 1 => { println("1111111") } case 2 => { println("2222222") } //使用守卫 case y if y==3 => { println("2222222") } case _ => { println("_") } } def test2(obj:Any) = obj match { case x:String => { println("is String") } case x:Int => { println("is Int") } case _ => { println("is some other") } } def test3(obj:Option[Any]) = obj match { case Some(_) => { println("is Some") } case None => { println("is None") } case _ => { println("is some other") } } }