大数据Spark “蘑菇云”行动前传第16课:Scala implicits编程彻底实战及Spark源码鉴赏
1 Spark源码Scala implicits的使用
2 Scala implicits实战
3 Scala implicits企业级最佳实践
package com.dt.spark.scala.bascis
class Man (val name:String)
/* 第一种方式
* object Man {
implicit def man2SuperMan(man:Man) = new SuperMan(man.name)
}*/
class SuperMan(val name:String){
def makeMiracles= println(this.name +" wow wow , i am superman")
}
object IMFimplicits {
//第二种方式
implicit def man2SuperMan(man:Man) = new SuperMan(man.name)
}
object HelloImplicits {
def main(args: Array[String]): Unit = {
import com.dt.spark.scala.bascis.IMFimplicits._
val man =new Man("scla")
man.makeMiracles
//做默认配置
implicit val content = "Dollar"
// talk("java")("money")
talk("java")
talk("java")("spark")
}
def talk(name:String)(implicit content :String) = println(name+ " : "+content)
}
运行
结果
scla wow wow , i am superman
java : Dollar
java : spark