Scala学习——隐式转换

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方法
}

 

posted @ 2019-04-09 16:11  技术小白丁  阅读(1530)  评论(0编辑  收藏  举报