用IDEA创建maven项目后,在main中新建scala目录,File->Project Structure->Modules设置scala目录为sources,
再在Libraries中添加Scala SDK。
在scla中新建com.victor.scala包,新建Implict.scala
package com.victor.scala
/**
* Company: Huazhong University of science and technology
* 华中科技大学电气学院聚变与等离子体研究所
* Version: V1.0
* Author: Victor
* Contact: 1650996069@qq.com 2018--2020
* Software: IntelliJ IDEA
* File: Implict
* Time: 2018/10/14 16:30
* Desc:
**/
class Implict {
}
class Test{
}
class RunTest(test: Test){
def run: Unit ={
println("RunTest-->test="+test)
}
}
object Implict extends App{
//隐式转换
implicit def test2RunTest(test: Test) = new RunTest(test)
val test = new Test
test.run
//隐式参数
def testParam(implicit name:String): Unit ={
println("name="+name)
}
implicit val name ="Implicted......."
testParam
//明显的指明参数
testParam("test wu ye .....")
//隐式类
implicit class ADD(x:Int){
def add(a:Int):Int = a+x
}
println("2.add(23)="+2.add(23))
//the same to next line
println("2.add(23)="+ADD(2).add(23))
}
=====================================================================
结果:
后续继续更新。