scala隐式转换

一、需求:为一个类添加一个新的方法

java:动态代理

scala:隐式转换

隐式转换例子:

1、man to superMan

package top.ruandb.scala.Course07

object ImplicitApp {

  def main(args: Array[String]): Unit = {
//定义隐式转换函数,用于将man成superMan
implicit def man2superman(man:Man):SuperMan=new SuperMan(man.name);
//实例化一个man
val man = new Man("rdb")
//返回的是一个超人
man.fly()
//t同时它也是一个人
man.eat("马铃薯")
}
}
class Man(val name:String){
def eat(food:String): Unit ={
println(s"${name} 正在吃 ${food}")
}
}
class SuperMan(val name:String){
def fly(): Unit ={
println(s"${name} is fly ......")
}
}

2、java IO中File类是没有read方法的,我们可以通过隐式转换添加read方法

package top.ruandb.scala.Course07
import java.io.File
object ImplicitApp { def main(args: Array[String]): Unit = { //定义隐式转换函数
implicit def file2richfile(file:File) :RichFile = new RichFile(file);
val file = new File("D:\\test\\a.txt")
//位File类增加了read方法
val txt = file.read()
println(txt)
}
}
class RichFile(val file:File){
def read(): String ={
scala.io.Source.fromFile(file.getPath).mkString
}
}

二、隐式转换切面封装

上面两个小例子中隐式转换函数和业务代码放一起了,正式情况下应该统一封装到一个切面中

package top.ruandb.scala.Course07

import java.io.File
//将切面引入
import top.ruandb.scala.Course07.ImplicitAspect._ object ImplicitApp { def main(args: Array[String]): Unit = { //实例化一个man
val man = new Man("rdb")
//返回的是一个超人
man.fly() //也可以用的使用引入,用哪个引入哪个
//import top.ruandb.scala.Course07.ImplicitAspect.file2richfile
val file = new File("D:\\test\\a.txt")
//位File类增加了read方法
val txt = file.read()
println(txt) }
}
class RichFile(val file:File){
def read(): String ={
scala.io.Source.fromFile(file.getPath).mkString
}
}
class Man(val name:String){
def eat(food:String): Unit ={
println(s"${name} 正在吃 ${food}")
}
} class SuperMan(val name:String){
def fly(): Unit ={
println(s"${name} is fly ......")
}
}
package top.ruandb.scala.Course07

import java.io.File

object ImplicitAspect {

  //定义隐式转换函数
implicit def file2richfile(file:File) :RichFile = new RichFile(file); //定义隐式转换函数,用于将man成superMan
implicit def man2superman(man:Man):SuperMan=new SuperMan(man.name);
}

三、隐式参数

指的是在函数或者方法中,定义一个implicit修饰的参数,此时scala会尝试找到一个指定类型的,用implicit修饰的对象,即隐式值,并注入参数

package top.ruandb.scala.Course07

import java.io.File
//将切面引入
import top.ruandb.scala.Course07.ImplicitAspect._ object ImplicitApp { def main(args: Array[String]): Unit = {
// testParam//会报错,找不到隐式参数 // testParam("lis")//会把lisi String当成隐式参数 // implicit val name = "lucy"
// testParam //会自动找到name 作为隐式参数 // implicit val s1 = "lucy"
// implicit val s2 = "lisi"
// testParam //报错,不知道用 s1 还是 s2 } def testParam(implicit name:String): Unit ={
println(name)
}
}

三、隐式类

对类增加implicit限定的类,主要作用是对类的加强

package top.ruandb.scala.Course07

object ImplicitClassApp extends App {

  //隐式类传进来是Int,会发现所有的Int都包含add方法
implicit class Calculator(x:Int){
def add(a:Int): Int = a + x
} println(12.add(3)) //默认情况下Int类里没有add方法
}

最新文章

  1. VC++获取IDC_EDIT的7种方法
  2. 链表的C++实现——创建-插入-删除-输出-清空
  3. [MongoDB]Mongodb攻略
  4. Generic method return type
  5. CSS3之选择器
  6. MATLAB中匿名函数与符号函数的转换
  7. 【JavaScript】AJAX教程
  8. 分享下VellLock源代码。。。VellLock正式开源
  9. ubuntu14.04下嵌入式工作环境搭建
  10. USACO Subset 整数划分01背包
  11. 使用EF连接Postgresql
  12. Vue-组件嵌套之——父组件向子组件传值
  13. Java基础-多线程-②多线程安全问题
  14. 基于bootstrap的后台左侧导航菜单和点击二级菜单刷新二级页面时候菜单展开显示当前菜单
  15. Dubbo浅谈
  16. UNIGUI换版本注意事项
  17. SSM框架-使用MyBatis Generator自动创建代码
  18. jenkins 构建一个maven项目
  19. datagrid分页 从后端获取数据也很简单
  20. juqery dragsort使用遇到的问题

热门文章

  1. BEP 7:CUDA外部内存管理插件(上)
  2. 编写可调模板并使用Auto-tuner自动调谐器
  3. 简化可视SLAM应用程序的开发
  4. springboot 集成 elk 日志收集功能
  5. 【NX二次开发】Block UI 多行字符串
  6. SpringBoot数据访问(二) SpringBoot整合JPA
  7. 4.6 Python3 进阶 - 递归函数
  8. Centos7安装部署搭建gitlab平台、汉化
  9. UnityPlayerActivity删除后的后果
  10. Unity中各种查找物体的方法