先看一个案例演示,引出隐式转换的实际须要=>指定某些数据类型的相互转化mysql
object boke_demo01 { def main(args: Array[String]): Unit = { val num: Int = 3.5 //?错 高精度->低精度 println(num) } }
隐式转换函数是以implicit关键字声明的带有单个参数的函数,这种函数将会自动应用,将值从一种类型转换为另外一种类型sql
使用隐式函数能够优雅的解决数据类型转换函数
案例演示scala
object boke_demo01 { def main(args: Array[String]): Unit = { //编写一个隐式函数转成 Double->Int 转换 //隐式函数应当在做用域才能生效 implicit def f1(d: Double): Int = { //底层 生成 f1$1 d.toInt } implicit def f2(f: Float): Int = { f.toInt } //这里咱们必须保证隐式函数的匹配只能是惟一的. // implicit def f3(f1:Float): Int = { // f1.toInt // } val num: Int = 3.5 // 底层编译 f1$1(3.5) val num2: Int = 4.5f // println("num =" + num) } }
-反编译后的代码对象
1) 隐式转换函数的函数名能够是任意的,隐式函数与函数名无关,只与函数签名(函数参数类型和返回值类型)有关blog
2) 隐式函数能够有多个(即:隐式函数列表),可是须要保证在当前环境下,只有一个隐式函数能被识别递归
使用隐式转换方式动态的给MySql类增长delete方法ci
object boke_demo01 { def main(args: Array[String]): Unit = { //编写一个隐式函数,丰富mySQL功能 implicit def addDelete(msql: MySQL): DB = { new DB } //建立mysql对象 val mySQL = new MySQL mySQL.insert() mySQL.delete() // 编译器工做 分析 addDelete$1(mySQL).delete() mySQL.update() } } class MySQL { def insert(): Unit = { println("insert") } } class DB { def delete(): Unit = { println("delete") } def update(): Unit = { println("update") } }
隐式值也叫隐式变量,将某个形参变量标记为implicit,因此编译器会在方法省略隐式参数的状况下去搜索做用域内的隐式值做为缺省参数作用域
object boke_demo01 { def main(args: Array[String]): Unit = { implicit val str1: String = "Jack" //这个就是隐式值 //implicit name: String :name就是隐式参数 def hello(implicit name: String): Unit = { println(name + " hello") } hello //底层 hello$1(str1); } }
//小结 //1. 当在程序中,同时有 隐式值,默认值,传值 //2. 编译器的优先级为 传值 > 隐式值 > 默认值 //3. 在隐式值匹配时,不能有二义性 //4. 若是三个 (隐式值,默认值,传值) 一个都没有,就会报错 object boke_demo01 { def main(args: Array[String]): Unit = { // 隐式变量(值) // implicit val name: String = "Scala" //implicit val name1: String = "World" //隐式参数 def hello(implicit content: String = "jack"): Unit = { println("Hello " + content) } //调用hello hello //当同时有implicit 值和默认值,implicit 优先级高 def hello2(implicit content: String = "jack"): Unit = { println("Hello2 " + content) } //调用hello hello2 //说明 //1. 当一个隐式参数匹配不到隐式值,仍然会使用默认值 implicit val name: Int = 10 def hello3(implicit content: String = "jack"): Unit = { println("Hello3 " + content) } //调用hello hello3 // hello3 jack // //当没有隐式值,没有默认值,又没有传值,就会报错 // def hello4(implicit content: String ): Unit = { // println("Hello4 " + content) // } //调用hello // hello4 // hello3 jack } }
在Scala2.10后提供了隐式类,可使用implicit声明类,隐式类很是强大,一样能够扩展类的功能,比前面使用隐式转换丰富类库功能更加的方便,在集合中隐式类会发挥重要做用编译器
1) 其所带的构造参数有且只能有一个
2) 隐式类必须被定义在“类”或“伴生对象”或“包对象”里,即隐式类不能是顶级的(top-level objects)
3) 隐式类不能是case class(样例类)
4) 做用域内不能有与之相同名称的标识符
隐式类的案例,进一步认识隐式类
object boke_demo01 { def main(args: Array[String]): Unit = { //DB会对应生成隐式类 //DB是一个隐式类, 当咱们在该隐式类的做用域范围,建立MySQL实例 //该隐式类就会生效, 这个工做仍然编译器完成 //看底层.. implicit class DB(val m: MySQL) { //boke_demo01$DB$1 def addSuffix(): String = { m + " scala" } } //建立一个MySQL实例 val mySQL = new MySQL mySQL.sayOk() mySQL.addSuffix() //研究 如何关联到 DB$2(mySQL).addSuffix(); implicit def f1(d: Double): Int = { d.toInt } def test(n1: Int): Unit = { println("ok") } test(10.1) } } class DB {} class MySQL { def sayOk(): Unit = { println("sayOk") } }
1) 当方法中的参数的类型与目标类型不一致时,或者是赋值时
implicit def f1(d: Double): Int = { d.toInt } def test1(n1: Int): Unit = { println("ok") } test1(9.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) 隐式操做不能嵌套使用 //好比:隐式函数转换
object boke_demo01 { def main(args: Array[String]): Unit = { //1. 隐式转换不能有二义性 //2. 隐式转换不能嵌套使用 implicit def f1(d: Double): Int = { d.toInt //val num2:Int = 2.3 //底层 f1$1(2.3) //f1$1对应的就是f1,就会造成递归 } val num1: Int = 1.1 } }