Scala 隐式转换 重要一点 什么事隐式转换
package com.xing.listfile/** * Created by DengNi on 2017/2/12. * 简要说明 implicit 是什么 ? 就是从一个类的对象调用另外一个类方法 *///Method 1 OptimusPrime can not call AutoBots 's function BlowFire// 增加 OptimusPrime object 对象 实现隐式转换class OptimusPrime(val function :String) //此处 val 是必须的 ,不然找不到 optimusPrime.function/**object OptimusPrime{ implicit def optimusPrime2AutoBots(optimusPrime:OptimusPrime) = new AutoBots(optimusPrime.function)}**/class AutoBots(function : String){ def blowFire = println(function + ": I can blow fire to fire you!!!")}//Method 2 定义一个不同类名字的 object 对象 ,zai main 方法里面引用这个 对象object implictis { implicit def optimusPrime2AutoBots(optimusPrime:OptimusPrime) = new AutoBots(optimusPrime.function)}object HelloImplicit { def main(args: Array[String]) { import implictis._ val op = new OptimusPrime("OptimusPrime") println(op.blowFire) //多参数 // talk("p1") 只传一个参数 指定是错的了 talk("p1")("p22") // 但是我们就只想传一个 呢?? implicit val name2 ="eat you" //此处也引用了 隐式转换 talk2("p3") } // 定义一个函数 有两个参数 def talk(name1 : String)(name2:String) = println("thera are 2 param") def talk2(name1 : String)(implicit name2:String) = println("thera are 2 param " +name2) // 此处 implicit 必须加}/**"C:\Program Files\Java\jdk1.7.0_80\bin\java" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_80\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\rt.jar;D:\bigdataworkspaces\kafSpSt\out\production\kafSpSt;F:\scala\lib\scala-actors-migration.jar;F:\scala\lib\scala-actors.jar;F:\scala\lib\scala-library.jar;F:\scala\lib\scala-reflect.jar;F:\scala\lib\scala-swing.jar;D:\bigdataworkspaces\kafSpSt\lib\fastjson-1.2.11.jar;D:\bigdataworkspaces\kafSpSt\lib\fastutil-7.0.11.jar;D:\bigdataworkspaces\kafSpSt\lib\kafka_2.10-0.8.2.1.jar;D:\bigdataworkspaces\kafSpSt\lib\mysql-connector-java-5.1.6.jar;D:\bigdataworkspaces\kafSpSt\lib\mysql-connector-java-5.1.8-bin.jar;D:\bigdataworkspaces\kafSpSt\lib\spark-assembly-1.6.0-hadoop2.6.0.jar;D:\bigdataworkspaces\kafSpSt\lib\spark-examples-1.6.0-hadoop2.6.0.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.xing.listfile.HelloImplicitOptimusPrime: I can blow fire to fire you!!!()thera are 2 paramthera are 2 param eat youProcess finished with exit code 0 **/