Scala之隐式转换
来源:互联网 发布:交互设计经典书籍 知乎 编辑:程序博客网 时间:2024/04/30 10:09
package com.uplooking.bigdata.p4.generic
import java.io.{BufferedReader, File, FileReader}
import scala.io.Source
/**
* Scala中的隐士转换
*/
object ImplicitOps {
implicit def double2Int(d:Double) = d.toInt
implicit def str2Int(str:String) = str.toInt
def main(args: Array[String]): Unit = {
// implictOps1
implictOps2
}
implicit def file2RichFile(file:File) = new RichFile(file)
class RichFile(val file:File) {
def read = Source.fromFile(file).getLines().mkString
}
def implictOps2: Unit = {
// val br:BufferedReader = new BufferedReader(new FileReader(""))
// var line:String = null
// while((line = br.readLine()) != null) {
// println(line)
// }
val file = new File("E:\\test\\scala\\wordcount.txt")
println(file.read)//---->调用了隐士转换
}
def implictOps1: Unit ={
val x:Int = 3.5
println("x====" + x)
//
val y:Int = "123456"
}
}
import java.io.{BufferedReader, File, FileReader}
import scala.io.Source
/**
* Scala中的隐士转换
*/
object ImplicitOps {
implicit def double2Int(d:Double) = d.toInt
implicit def str2Int(str:String) = str.toInt
def main(args: Array[String]): Unit = {
// implictOps1
implictOps2
}
implicit def file2RichFile(file:File) = new RichFile(file)
class RichFile(val file:File) {
def read = Source.fromFile(file).getLines().mkString
}
def implictOps2: Unit = {
// val br:BufferedReader = new BufferedReader(new FileReader(""))
// var line:String = null
// while((line = br.readLine()) != null) {
// println(line)
// }
val file = new File("E:\\test\\scala\\wordcount.txt")
println(file.read)//---->调用了隐士转换
}
def implictOps1: Unit ={
val x:Int = 3.5
println("x====" + x)
//
val y:Int = "123456"
}
}
0 0
- Scala之隐式转换
- Scala之隐式转换
- Scala之隐式转换
- Scala之隐式转换
- scala的隐式转换之二
- Scala之旅-隐式转换
- scala基础之隐式转换
- Scala隐式转换之隐式转换函数
- scala-隐式转换
- scala 隐式转换
- Scala隐式转换
- Scala隐式转换
- scala隐式转换
- scala隐式转换
- scala 隐式转换
- scala隐式转换
- scala隐式转换
- scala隐式转换
- 解决VS中scanf printf报错问题
- java中equals和==的区别
- 线性表的插入删除(java实现)
- java学习笔记4.5
- 用C语言写程序设计大作业之小火车
- Scala之隐式转换
- 【C语言】将一数组元素复制到另一个数组中
- java多线程基础实例代码
- 设置ASP.NET Wep API在Chrome浏览器中返回JSON
- 论文引介 | Understanding Neural Networks
- ORB特征点检测与描述详解
- 网址收藏
- Android 动画--帧动画(Frame Animation)
- 收集常用的正则表达式