隐式值和隐式参数
package com.yxx
object Test_Implicit {
def say(implicit name:String): Unit ={
println(s"$name is saying...")
}
def say1(age:Int)(implicit name:String): Unit ={
println(s"$name is saying ,$name 已经 $age 岁了")
}
def main(args: Array[String]): Unit = {
implicit val name:String ="alalal"
say
say("alalal")
say1(10)
say1(100)("kakakka")
}
}
隐式转换函数
package com.yxx
case class Fish(name:String){
def swim(): Unit ={
println(s"$name is swimming...")
}
}
case class Rabbit(name:String){
}
object Test_Implicit2 {
implicit def lslls(rabbit:Rabbit):Fish={
Fish(rabbit.name)
}
def main(args: Array[String]): Unit = {
val rabbit =Rabbit("lall")
rabbit.swim
}
}
隐式类
package com.yxx
case class Rabbit444(name:String)
object Test_Implicit3 {
implicit class A(r:Rabbit444){
def say(): Unit ={
println(s"${r.name} is saying...")
}
}
def main(args: Array[String]): Unit = {
val r =Rabbit444("lalallalal")
r.say()
}
}