1
2
3
4
5
object Implicit extends App {
implicit def int2str(i: Int): String = {i.toString}
def a(str: String) {println(str)}
a(1)
}

2.19. implicit隐式转换

用途:
l 把一种object类型安全地自动转换成另一种object类型;
l 不改动已有class设计即可添加新的方法;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
object ImplictDemo {

object Context{
implicit val ccc:String = "implicit"
}


object Param{
def print(content:String)(implicit prefix:String){
println(prefix+":"+content)
}
}

def main(args: Array[String]) {
Param.print("jack")("hello")

import Context._
Param.print("jack")
}

}