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 @   技术小白丁  阅读(1536)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示