1,隐式转换,隐式类,隐式参数,隐式对象
----你可以指定将某种类或者对象转换为其它类型的类或者对象
----转换原因,我们在开发的时候,需要提升某些类的功能(这些类本身没有这些功能),我们想让类拥有这些功能,可以使用隐式转换,提升类的功能,使用之后这些功能可以被收回。
implicit coversion function
implicit def function
----隐式转换,指定好后是Scala运行时自动调用的。
----隐式转换函数命名通用规则: implicit def intTointWritable(i:Int) = new intTointWritable()
scala> class Person (val name:String){
| }
defined class Person
scala> class Engineer(val name:String , val salary:Double){
| def code = {println(name + ":" +salary)}
| }
defined class Engineer
scala> implicit def person2Engineer(p:Person) ={
| new Engineer(p.name,1000000)
|
| }
warning: there were 1 feature warning(s); re-run with -feature for details
person2Engineer: (p: Person)Engineer
scala> val p = new Peson("Spark")
<console>:23: error: not found: type Peson
val p = new Peson("Spark")
^
scala> val p = new Person("Spark")
p: Person = $iwC$$iwC$Person@296df0a8
scala> p.code
Spark:1000000.0