Scala隐式转换之隐式参数

时间:2023-02-08 23:13:50
/**
* 隐式转换函数——隐式参数
*/
class SingPen {
def write(content: String): Unit = {
println(content)
}
}

object ImplicitDemo3 {
// def exam(name: String, pen: SingPen): Unit = {
//被implicit修饰的参数就是隐式参数
def exam(name: String)(implicit pen: SingPen): Unit = {
pen.write(name)
}

def main(args: Array[String]): Unit = {
implicit val pen = new SingPen
exam("李四")
exam("张三")
// exam("李四", pen)
// exam("王五", pen)
}
}