package hfd
object Test35 {
case class Person(name:String)
case class Student(name:String,className:String)
//match case 能根据 类名和属性的信息,匹配到对应的类
//注意:
//1 匹配的时候,case class的属性个数要对上
//2 数学名不需要一一对应
def main(args: Array[String]): Unit = {
val p1=Person("小花")
val s1=Student("小明","2(3)班")
val a:Any=p1
a match {
case Person(name)=>println("普通人类",name)
case Student(name,className)=>println(className,"的学生",name)
case _=>println("其他")
}
}
}
Scala的模式匹配(7)
2401_833788052024-12-05 12:47