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"
}
}