隐式转换

//隐式转换 将file =>RichFile    MyPreDef 是门面
import java.io.File
import scala.io.Source

 object  MyPreDef {
   implicit def readFile(f:File) = new RichFile(f)
 }
class RichFile(val f: File) {
  def read() = Source.fromFile(f).mkString
}
object RichFile {
  def main(args: Array[String]): Unit = {
    val f = new File("path")
    //val content = new RichFile(f).read //显示定义
    import MyPreDef.readFile
    f.read //隐式定义
  }
}

你可能感兴趣的:(隐式转换)