package com.dtspark.scala.basics
/**
* Implicits隐式转换实战
*/
class Man(val name:String)
/*object Man{
implicit def man2SuperMan(man:Man)=new SuperMan(man.name)
}*/
object implicits{
implicit def man2SuperMan(man:Man)=new SuperMan(man.name)
}
class SuperMan(val name:String){
def MakeMiracles=println(this.name+"Wow,wow,wow...")
}
object HelloImplicits {
def main(args: Array[String]): Unit = {
//导入implicits包
import com.dtspark.scala.basics.implicits._
//实例化伴生类
val man=new Man("Scala")
//执行方法
man.MakeMiracles
}
implicit val content="hello"
talk("Scala")("Spark")
def talk(name:String)(implicit content:String)=println(name + ":" +content)
}
运行结果:
Scala:Spark
ScalaWow,wow,wow...
时间: 2024-10-16 06:32:43