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...
本文出自 “8381513” 博客,谢绝转载!
蘑菇云行动前传第16课:Scala implicits编程彻底实战及Spark源码鉴赏
原文地址:http://8391513.blog.51cto.com/8381513/1835457