zoukankan      html  css  js  c++  java
  • 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)

      

  • 相关阅读:
    初试 Elastic Search
    索引分类
    Nginx
    LINQ入门
    CSS学习
    Keras 安装
    火车进站
    2016年网易笔试编程题2
    Java GC
    linux 安装 mysql
  • 原文地址:https://www.cnblogs.com/hejunhong/p/14211761.html
Copyright © 2011-2022 走看看