package com.test
import scala.util.control.Breaks
import scala.io.Source
import java.io.File
/**
* 隐士类
*/
object Test {
implicit class FileEnch(file: File) {
def read: String = Source.fromFile(file.getPath).mkString
}
implicit class Fir(x: Int) {
def addSAP(second: Int): Int = x + second
}
}
/**
* 隐式值
*/
object Test1 {
def person(implicit name: String): String = name //name隐式参数
implicit val p: String = "mobin"
}
/**
* 隐式类型
*/
object Test2 {
def foo(msg: String): Unit = println(msg)
implicit def intToString(x: Int) = x.toString()
}
/**
* 测试主方法
*/
object implicit_Class {
def main(args: Array[String]) {
//隐士类
import Test._
println(1.addSAP(2))
println(new File("content.txt").read)
//隐式值
import Test1._
println(person)
//隐式类型
import Test2._
println(foo(2))
}
}
implicit隐式转换
最新推荐文章于 2021-02-24 22:42:37 发布