Scala中的显示转换和隐式转换

Scala中的显示转换和隐式转换

隐式转换----隐式类型转换,不需要手动转换
显示转换----显示类型转换,需要手动转换

1、显示转换

object Demo29 {
  def main(args: Array[String]): Unit = {
	//定义一个字符串变量
    val s = "100"
    //将字符串变量转换为Int类型
    val i: Int = s.toInt//显示转换

	//定义一个方法
    def print(s: String): Unit = {
      println(s)
    }
      
    //调用print方法
    print("java")
    //显示转换
    print(100.toString)
    }
}

2、隐式转换----关键字:implicit

1、隐式转换方法

需要在main方法中,自定义隐式转换方法

package com.shujia.scala

object Demo29 {
  def main(args: Array[String]): Unit = {
    /**
     * 1、隐式转换方法
     * 隐式转换只在当前作用域中起作用
     *
     * 隐式转换方法,可以隐式的将参数类型转换成返回值类型,
     * 当前作用域中所有的参数类型的变量都可以当成返回值类型来使用
     *
     * 隐式转换方法和方法名无关, 和参数类型返回值类型有关,
     * 在同一个作用域中只能存储一种(参数类型和返回值类型一样的)隐式转换
     *
     */

    //定义隐式转换方法
    implicit def intToString(i: Int): String = {  //当前作用域的所有Int类型的变量,都可以当成String类型使用
      println("隐式转换方法被调用")
      i.toString
    }

    implicit def intToDouble(i: Int): Double = {  //当前作用域的所有Int类型的变量,都可以当成Double类型使用
      println("隐式转换方法被调用")
      i.toDouble
    }

    implicit def doubleToString(i: Double): String = {  //当前作用域的所有Double类型的变量,都可以当成String类型使用
      println("隐式转换方法被调用")
      i.toString
    }

    //定义一个函数
    def print(s: String): Unit = {
      println(s)
    }
    //调用这个函数,需要传入一个String类型的参数
    //由于前面我们定义了隐式转换方法--intToString()
    //所以我们参数传入一个Int类型的也可以,因为隐式转换方法会帮我们自动转换
    print(888)
  }
}
2、隐式转换类

需要在object中自定义隐式转换类

package com.shujia.scala

import scala.io.Source

object Demo30 {
  def main(args: Array[String]): Unit = {
	//创建类的对象,需要传入一个String类型的路径
    val fileRead = new FileRead("data/students.txt")
    //根据类的对象名调取该类中的方法
    val student: List[String] = fileRead.read()
    println(student)
  }

//定义一个普通的类,类中有一个read()方法
//read()方法中,可以根据路径获取行数据
class FileRead(path: String) {
    def read(): List[String] = {
      Source.fromFile(path).getLines().toList
    }
  }
    
}

使用隐式转换类

package com.shujia.scala

import scala.io.Source

object Demo30 {
  def main(args: Array[String]): Unit = {
      
    //定义path的路径,类型为String
    val path = "data/students.txt"
      
    //String 类型被隐式转换成了FIleRead 类型,所以String 有了Read方法
    //所以可以之间调用该隐式类中的方法
    val list: List[String] = path.read()
或者
    val words: List[String] = "data/words.txt".read()
  }

  /**
   * 隐式转换类可以隐式的将类的构造函数类型转换成当前类的类型
   *
   * String  -- 隐式转换-->   FileRead
   */
  implicit class FileRead(path: String) {
    def read(): List[String] = {
      Source.fromFile(path).getLines().toList
    } 
  }
    
}
3、隐式转换参数、函数柯里化

隐式转换参数:不做转换,它是方法调用的简化

object Demo31{
  def main(args: Array[String]): Unit = {
	  //定义一个函数
      def add1(x:Int,y:Int): Int ={
        x+y
      }
      
    //调用函数,需要传入两个参数
    println(add1(10,20))  //30
  }
}

隐式转换参数结合函数柯里化使用

object Demo31{
  def main(args: Array[String]): Unit = {

    //函数柯里化,将两个参数分开,并给后面的参数加上implicit
    def add(x: Int)(implicit y: Int): Int = {
      x + y
    }

  //定义隐式转换变量
  implicit val i: Int = 20  //同意作用域中不能出现两个类型一样的隐式转换变量
  //在调用方法的时候,会自动使用当前作用域中的隐式转换变量,填充方法的隐式转换参数,类型要一致
  println(add(10))  //30 只需要传入一个参数即可,该参数表示x的值,y的值就是隐式转换变量的值
      
   //应用
  //读取文件数据
  val source: BufferedSource = Source.fromFile("data/students.txt")

 //在读取文件数据的时候,只需要传入一个参数(文件路径)即可
 //实际上,底层使用了隐式转换变量,不需要我们在写出第二个参数
 //全称:
//    al source: BufferedSource = Source.fromFile("data/students.txt")(Codec("utf-8")) 
      
  }
}
posted @   阿伟宝座  阅读(101)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示