概述
本文涉及:
模式匹配中 数组参数处理
继承类处理
参数和对象同时获取
代码
package com.yy.ceshi.ceshi
object Test {
def main(args: Array[String]): Unit = {
def size(s: Shape): Unit = s match {
// @ 表示把右侧绑定到左侧 相当于变量定义 即 t=Rectangle(x, y)
case t@Rectangle(x, y) => println(f"i am ${t}")
// 即可以获取对应给变量t 也可以获取参数 r
case t@Circle(r) => println(f"i am ${t}, my args is $r")
case t@Location(x, y, shape) => println(f"i am ${t}")
// @_* 表示把参数当作数组给变量ss
case Group(ss@_*) => ss.foreach(size)
}
size(Group(Circle(1), Rectangle(11, 22)))
Circle(1) match {
// @_ 表示把匹配项直接给ss
case ss@_ => println(ss) // Circle(1)
}
}
}
/**
* 输出:
* i am Circ