object test7 {
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(nm) => println("普通人类",nm)
case Student(name,className) => println(className,"的学生",name)
case _ => println("其他")
}
}
}
Scala的match case 能根据 类名和属性的信息,匹配到对应的类
懒惰的橘猫2024-12-07 8:57