Scala之隐式转换
2017-04-09 15:35
169 查看
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"
}
}
相关文章推荐
- scala隐式转换
- scala中隐式转换之隐式转换调用类中本不存在的方法
- scala隐式转换
- scala进阶18-隐式转换-隐式参数
- Scala入门系列(十二):隐式转换
- scala中的隐式转换详解
- Scala 中的隐式转换 implicit
- scala 隐式转换(一)
- scala入门-10 隐式转换、隐式参数、隐式类
- scala_集合_泛型_隐式转换
- scala(7)隐式转换 隐式变量 隐式类
- Scala学习教程笔记三之函数式编程、集合操作、模式匹配、类型参数、隐式转换、Actor、
- Scala中的隐式转换、隐式参数、隐式类
- Scala之旅-隐式转换
- scala-隐式转换
- Scala泛型、隐式转换和隐式参数、视图介绍、Scala中的上界、下界、结合柯里化进行隐式转换
- Scala 隐式转换案例(三)
- Scala中隐式转换(implicit conversion)的优先顺序
- scala中隐式转换之隐式类
- Scala 隐式转换