匹配前提必须是 case 中的子类或者父类

  1. object Test3{
  2. def main(args: Array[String]): Unit = {
  3. def matchClass(x:Any):String={
  4. x match {
  5. case i:Array[Int] => "array"
  6. case i:List[Int] => "list"
  7. case _ => "Nothing"
  8. }
  9. }
  10. println(matchClass(Array(1, 2, 3)))
  11. println(matchClass(List(1, 2, 3)))
  12. }
  13. }