scala模式匹配 case a @ b语法

class caseTest {
  def main(args: Array[String]): Unit = {

    val c = Person(Student(1),"a")
    c match {
      //匹配Person对象,但除了使用Person对象的两个构造参数stu, name外
      //有时候还需要用到匹配Person对象实例, 使用 a@A a为对象名,A为对象类型
      case person @ Person(stu, name) => {
        val student: Student = person.stu
        val clazz = person.getClass
        val age1 = student.age
     
        println(age1)
      }
        //普通的对象模式匹配
      case Person(stu, name) => {
        println(stu, name)
      }
    }

  }
}

case class Person(stu: Student, name: String)

case class Student(age: Int)

  

 

posted @ 2020-12-30 15:41  夜半钟声到客船  阅读(510)  评论(0编辑  收藏  举报