1、代码
package com.yy.enhance
import java.io.File
import scala.io.Source
/**
* 隐式类
*/
object Context{
//隐式类
implicit class FileEnhancer(file:File){
def read = Source.fromFile(file.getPath(),"GBK").mkString
}
//隐式类x+y
implicit class Op(x:Int){
def add(y:Int) = x + y
}
}
object ImplicitClassTest extends App {
import Context._
println(new File("D:\\ww\\test.txt").read)
println(1.add(2))
}
2、结果
this is the first line of this txt file.
您好,世界。
我是一只小小鸟
残缺的孤独
3