经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Scala » 查看文章
9. Scala隐式转换和隐式值
来源:cnblogs  作者:铖歌  时间:2019/5/8 9:28:58  对本文有异议

9.1 隐式转换

  9.1.1 提出问题

      先看一个案例演示,引出隐式转换的实际需要=>指定某些数据类型的相互转化

  1. object boke_demo01 {
  2.  
  3. def main(args: Array[String]): Unit = {
  4.  
  5. val num: Int = 3.5 //?错 高精度->低精度
  6. println(num)
  7. }
  8. }

  9.1.2 隐式函数基本介绍

      隐式转换函数是以implicit关键字声明的带有单个参数的函数,这种函数将会自动应用,将值从一种类型转换为另一种类型

  9.1.3 隐式函数快速入门

      使用隐式函数可以优雅的解决数据类型转换

      案例演示

  1. object boke_demo01 {
  2.  
  3. def main(args: Array[String]): Unit = {
  4.  
  5. //编写一个隐式函数转成 Double->Int 转换
  6. //隐式函数应当在作用域才能生效
  7. implicit def f1(d: Double): Int = { //底层 生成 f1$1
  8. d.toInt
  9. }
  10.  
  11. implicit def f2(f: Float): Int = {
  12. f.toInt
  13. }
  14.  
  15. //这里我们必须保证隐式函数的匹配只能是唯一的.
  16. // implicit def f3(f1:Float): Int = {
  17. // f1.toInt
  18. // }
  19.  
  20.  
  21. val num: Int = 3.5 // 底层编译 f1$1(3.5)
  22. val num2: Int = 4.5f //
  23. println("num =" + num)
  24.  
  25. }
  26. }

      -反编译后的代码

      

  9.1.4 隐式转换的注意事项和细节  

      1) 隐式转换函数的函数名可以是任意的,隐式函数与函数名无关,只与函数签名(函数参数类型和返回值类型)有关

      2) 隐式函数可以有多个(即:隐式函数列表),但是需要保证在当前环境下,只有一个隐式函数能被识别

9.2 隐式转换丰富类库功能 

  9.2.1 快速入门案例 

      使用隐式转换方式动态的给MySql类增加delete方法

  9.2.2 案例演示 

  1. object boke_demo01 {
  2.  
  3. def main(args: Array[String]): Unit = {
  4. //编写一个隐式函数,丰富mySQL功能
  5. implicit def addDelete(msql: MySQL): DB = {
  6. new DB
  7. }
  8.  
  9. //创建mysql对象
  10. val mySQL = new MySQL
  11. mySQL.insert()
  12. mySQL.delete() // 编译器工作 分析 addDelete$1(mySQL).delete()
  13. mySQL.update()
  14.  
  15. }
  16. }
  17.  
  18. class MySQL {
  19. def insert(): Unit = {
  20. println("insert")
  21. }
  22. }
  23.  
  24. class DB {
  25. def delete(): Unit = {
  26. println("delete")
  27. }
  28.  
  29. def update(): Unit = {
  30. println("update")
  31. }
  32. }

9.3 隐式值 

  9.3.1 基本介绍 

      隐式值也叫隐式变量,将某个形参变量标记为implicit,所以编译器会在方法省略隐式参数的情况下去搜索作用域内的隐式值作为缺省参数

  9.3.2 快速入门案例  

  1. object boke_demo01 {
  2. def main(args: Array[String]): Unit = {
  3.  
  4. implicit val str1: String = "Jack" //这个就是隐式值
  5.  
  6. //implicit name: String :name就是隐式参数
  7. def hello(implicit name: String): Unit = {
  8. println(name + " hello")
  9. }
  10.  
  11. hello //底层 hello$1(str1);
  12.  
  13. }
  14. }

  9.3.3 一个案例说明隐式值,默认值,传值的优先级 

  1. //小结
  2. //1. 当在程序中,同时有 隐式值,默认值,传值
  3. //2. 编译器的优先级为 传值 > 隐式值 > 默认值
  4. //3. 在隐式值匹配时,不能有二义性
  5. //4. 如果三个 (隐式值,默认值,传值) 一个都没有,就会报错
  6.  
  7. object boke_demo01 {
  8. def main(args: Array[String]): Unit = {
  9. // 隐式变量(值)
  10. // implicit val name: String = "Scala"
  11. //implicit val name1: String = "World"
  12.  
  13. //隐式参数
  14. def hello(implicit content: String = "jack"): Unit = {
  15. println("Hello " + content)
  16. } //调用hello
  17. hello
  18.  
  19. //当同时有implicit 值和默认值,implicit 优先级高
  20. def hello2(implicit content: String = "jack"): Unit = {
  21. println("Hello2 " + content)
  22. } //调用hello
  23. hello2
  24.  
  25.  
  26. //说明
  27. //1. 当一个隐式参数匹配不到隐式值,仍然会使用默认值
  28.  
  29. implicit val name: Int = 10
  30.  
  31. def hello3(implicit content: String = "jack"): Unit = {
  32. println("Hello3 " + content)
  33. } //调用hello
  34. hello3 // hello3 jack
  35.  
  36. // //当没有隐式值,没有默认值,又没有传值,就会报错
  37. // def hello4(implicit content: String ): Unit = {
  38. // println("Hello4 " + content)
  39. // } //调用hello
  40. // hello4 // hello3 jack
  41. }
  42. }

9.4 隐式类 

  9.4.1 基本介绍 

      在Scala2.10后提供了隐式类,可以使用implicit声明类,隐式类非常强大,同样可以扩展类的功能,比前面使用隐式转换丰富类库功能更加的方便,在集合中隐式类会发挥重要作用

  9.4.2 隐式类使用有如下几个特点 

      1) 其所带的构造参数有且只能有一个

      2) 隐式类必须被定义在“类”或“伴生对象”或“包对象”里,即隐式类不能是顶级的(top-level objects)

      3) 隐式类不能是case class(样例类)

      4) 作用域内不能有与之相同名称的标识符

  9.4.3 应用案例 

      隐式类的案例,进一步认识隐式类

  1. object boke_demo01 {
  2.  
  3. def main(args: Array[String]): Unit = {
  4. //DB会对应生成隐式类
  5. //DB是一个隐式类, 当我们在该隐式类的作用域范围,创建MySQL实例
  6. //该隐式类就会生效, 这个工作仍然编译器完成
  7. //看底层..
  8. implicit class DB(val m: MySQL) { //boke_demo01$DB$1
  9. def addSuffix(): String = {
  10. m + " scala"
  11. }
  12. }
  13.  
  14.  
  15. //创建一个MySQL实例
  16. val mySQL = new MySQL
  17. mySQL.sayOk()
  18. mySQL.addSuffix() //研究 如何关联到 DB$2(mySQL).addSuffix();
  19.  
  20. implicit def f1(d: Double): Int = {
  21. d.toInt
  22. }
  23.  
  24. def test(n1: Int): Unit = {
  25. println("ok")
  26. }
  27.  
  28. test(10.1)
  29.  
  30. }
  31. }
  32.  
  33. class DB {}
  34.  
  35.  
  36. class MySQL {
  37. def sayOk(): Unit = {
  38. println("sayOk")
  39. }
  40. }

9.5 隐式的转换时机 

      1) 当方法中的参数的类型与目标类型不一致时,或者是赋值时

  1. implicit def f1(d: Double): Int = {
  2. d.toInt
  3. }
  4.  
  5. def test1(n1: Int): Unit = {
  6. println("ok")
  7. }
  8.  
  9. test1(9.1)

      2) 当对象调用所在类中不存在的方法或成员时,编译器会自动将对象进行隐式转换(根据类型)

9.6 隐式解析机制 

      即编译器是如何查找到缺失信息的,解析具有以下两种规则:

      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 都会被搜索

9.7 在进行隐式转换时,需要遵守两个基本的前提 

      1) 不能存在二义性

      2) 隐式操作不能嵌套使用  //比如:隐式函数转换

  1. object boke_demo01 {
  2. def main(args: Array[String]): Unit = {
  3.  
  4. //1. 隐式转换不能有二义性
  5. //2. 隐式转换不能嵌套使用
  6.  
  7. implicit def f1(d: Double): Int = {
  8. d.toInt
  9. //val num2:Int = 2.3 //底层 f1$1(2.3) //f1$1对应的就是f1,就会形成递归
  10. }
  11.  
  12. val num1: Int = 1.1
  13. }
  14. }

 

  

原文链接:http://www.cnblogs.com/zhanghuicheng/p/10816538.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号