Scala-隐式转换

打印 上一主题 下一主题

主题 1005|帖子 1005|积分 3015

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
隐式转换

精度小的类型可以自动转换为精度大的类型,这个转换过程无需开发人员参与,由编译器自动完成,这个转换操作我们称之为隐式转换。

  • 如果程序编译出错,编译器会尝试在整个的作用域中查找能够让程序编译通过的方式
  • 如果找到,那么编译器会尝试二次编译,让之前编译出现错误的代码经过转换后能够编译通过。
  • 这个转换过程看不见,但是存在,就称之为隐式转换,所谓的隐式转换,其实就是类型的转换
隐式函数
  1. object ScalaImplicit {
  2.     def main(args: Array[String]): Unit = {
  3.         implicit def transform( d : Double ): Int = {
  4.             d.toInt
  5.         }
  6.         var d : Double = 2.0
  7.         val i : Int = d
  8.         println(i)
  9.     }
  10. }
复制代码
隐式函数实现程序功能的扩展

符合OCP
  1.     def main(args: Array[String]): Unit = {
  2.         // 使用implicit声明的函数为隐式函数。
  3.         implicit def thransform( user:User ): UserExt = {
  4.             new UserExt()
  5.         }
  6.         val user = getUser()
  7.         user.insertUser()
  8.         user.updateUser()
  9.     }
  10.     class UserExt {
  11.         def updateUser(): Unit = {
  12.             println("update user...")
  13.         }
  14.     }
  15.     class User {
  16.         def insertUser(): Unit = {
  17.             println("insert user...")
  18.         }
  19.     }
  20.     def getUser() = {
  21.         new User()
  22.     }
复制代码
隐式参数
  1. def reg( implicit password : String = "000000" ): Unit = {
  2.     println("默认密码:" + password)
  3. }
复制代码
隐式变量


  • 隐式参数不用传递,这个传递的过程由编译器完成
  • 在同一个作用域中,如果相同的转换规则的多个数据,会发生错误
  1. implicit val password : String = "111111"
  2. reg ==> 默认密码:111111
复制代码
隐式类

在Scala2.10后提供了隐式类,可以使用implicit声明类,隐式类非常强大,同样可以扩展类的功能,在集合的数据处理中,隐式类发挥了重要的作用。

  • 其所带的构造参数有且只能有一个
  • 隐式类必须被定义在“类”或“伴生对象”或“包对象”里,即隐式类不能是顶级的。
  1. def main(args: Array[String]): Unit = {
  2.         val user = new User()
  3.     user.insertUser()
  4.     user.updateUser()
  5. }
  6. implicit class UserExt(user:User) {
  7.     def updateUser(): Unit = {
  8.         println("update user...")
  9.     }
  10. }
  11. class User {
  12.     def insertUser(): Unit = {
  13.         println("insert user...")
  14.     }
复制代码
隐式机制


  • 当前代码作用域
    1. implicit class UserExt(user:User) {
    2.     def updateUser(): Unit = {
    3.         println("update user...")
    4.     }
    5. }
    复制代码
  • 父类或伴生对象
    1. class Parent {
    2.     implicit class UserExt(user:User) {
    3.         def updateUser(): Unit = {
    4.             println("update user...")
    5.         }
    6.     }
    7. }
    8. object Parent {
    9.     implicit class UserExt(user:User) {
    10.         def updateUser(): Unit = {
    11.             println("update user...")
    12.         }
    13.     }
    14. }
    复制代码
  • 特征或伴生对象
    1. trait MyTrait {
    2.     implicit class UserExt(user:User) {
    3.         def updateUser(): Unit = {
    4.             println("update user...")
    5.         }
    6.     }
    7. }
    8. object MyTrait {
    9.     implicit class UserExt(user:User) {
    10.         def updateUser(): Unit = {
    11.             println("update user...")
    12.         }
    13.     }
    复制代码
  • 在其他地方声明(包对象)
    1. package object chapter {
    2.     implicit class UserExt(user:User) {
    3.         def updateUser(): Unit = {
    4.             println("update user...")
    5.         }
    6.     }
    7. }
    复制代码
  • 直接导入
    1. import com.atguigu.bigdata.scala.test.TestTransform._
    复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

花瓣小跑

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表