Scala隱式轉換和作用域分析
1 Scala隱式轉換內幕
2 Scala作用域
3 Scala隱式轉換思考
/**
* 隱式轉換和作用域
*/
class Implicits(x: Int) {
def printValue = {
val x = 100
println(x)
}
}
class RicherFile(val file: java.io.File) {
def dtspark = println("hello spark")
}
object Implicits {
implicit val content1 = "Implicits Java scala spark"
implicit def int2String(x: Int) = x.toString()
implicit def file2RicherFile(file: java.io.File) = new RicherFile(file)
implicit def richerFile2File(richerFile: RicherFile) = richerFile.file
}
object ImplicitsMsg {
implicit val msg = " ImplicitsMsg scala spark"
}
object ImplicitAdvanced{
def main(args: Array[String]): Unit = {
print("-------------")
def printContent(implicit content: String) = println(content)
val content = "i love spark"
import Implicits._
//import Implicits.content1優先順序高於下面的作用域
// import ImplicitsMsg._
printContent(100)
new java.io.File(".").dtspark
}
}