隐式转换函数是以implicit关键字声明的带有单个参数的函数。这种函数将会自动应用,java
将值从一种类型转换为另外一种类型。ide
implicit def a(d: Double) = d.toInt
//不加上边这句你试试 val i1: Int = 3.5 println(i1)
若是须要为一个类增长一个方法,能够经过隐式转换来实现。好比想为 File 增长一个 read 方法,函数
能够以下定义:spa
import java.io.File import scala.io.Source class RichFile(val from: File) { def read = Source.fromFile(from.getPath).mkString } object Main1 extends App{ implicit def file2RichFile(from: File) = new RichFile(from) val contents = new File("D:\\Data\\Scala\\随堂笔记\\笔记").read println(contents) }
有什么好处呢?好处就是你能够不修改原版本的代码而为本来的代码增长新功能。scala
将name变量标记为implicit,因此编译器会在方法省略隐式参数的状况下去搜索做用域内的隐式值做为缺乏参数。code
implicit val name = "Nick"
def person(implicit name: String) = name println(person) //Nick
可是若是此时你又相同做用域中定义一个隐式变量,再次调用方法时就会报错:对象
implicit val name = "Nick" implicit val name2 = "Nick"
def person(implicit name: String) = name println(person) //报错!
1) 隐式转换为目标类型:把一种类型自动转换到另外一种类型blog
def foo(msg : String) = println(msg)
implicit def intToString(x : Int) = x.toString foo(10)
2) 隐式转换调用类中本不存在的方法ci
class Dog { val name = "金毛" } class Skill{ def fly(animal: Dog, skill: String) = println(animal.name + "已领悟" + skill) } object Learn{ implicit def learningType(s : Dog) = new Skill } object Main2 extends App{ override def main(args: Array[String]): Unit = { import unit14.Learn._ val dog = new Dog dog.fly(dog, "飞行技能") } }
import unit14.Learn._ class Dog { val name = "金毛" } class Skill{ def fly(animal: Dog, skill: String) = println(animal.name + "已领悟" + skill) def Q() = println("Q谁谁怀孕") def W() = println("W谁谁怀孕") } object Learn{ implicit def learningType(s : Dog) = new Skill } object Main2 extends App{ override def main(args: Array[String]): Unit = { val dog = new Dog dog.fly(dog, "飞行技能") dog.Q() dog.W() } }
固然了,以上操做也能够定义在包对象中,即,在object Learn的外面再套一层,package,没问题的!作用域
在scala2.10后提供了隐式类,可使用implicit声明类,可是须要注意如下几点:
—– 其所带的构造参数有且只能有一个 —– 隐式类必须被定义在“类”或“伴生对象”或“包对象”里 —– 隐式类不能是case class(case class在定义会自动生成伴生对象与2矛盾) —– 做用域内不能有与之相同名称的标示符
object StringUtils { implicit class StringImprovement(val s : String){ //隐式类 def increment = s.map(x => (x + 1).toChar) } }
object Main3 extends App{
import unit14.StringUtils._
println("abcd".increment) //bcde }
1) 当方法中的参数的类型与目标类型不一致时
2) 当对象调用所在类中不存在的方法或成员时,编译器会自动将对象进行隐式转换
即编译器是如何查找到缺失信息的,解析具备如下两种规则:
1) 首先会在当前代码做用域下查找隐式实体(隐式方法、隐式类、隐式对象)。
2) 若是第一条规则查找隐式实体失败,会继续在隐式参数的类型的做用域里查找。
类型的做用域是指与该类型相关联的所有伴生模块,一个隐式实体的类型T它的查找范围以下:
a) 若是T被定义为T with A with B with C,那么A,B,C都是T的部分,在T的隐式解析过程当中,它们的伴生对象都会被搜索。
b) 若是T是参数化类型,那么类型参数和与类型参数相关联的部分都算做T的部分,好比 List[String] 的隐式搜索会搜索 List 的伴生对象和 String 的伴生对象。
c) 若是T是一个单例类型 p.T,即T是属于某个 p 对象内,那么这个p对象也会被搜索。
d) 若是T是个类型注入 S#T,那么 S 和 T 都会被搜索。
1) 不能存在二义性
2) 隐式操做不能嵌套