雲星數據---Scala實戰系列(精品版)】:Scala入門教程052-Scala實戰源碼-Scala implicit 操作03

Scala implicit 操作03

package scala_learn.demo12_Implicit

/**
 * Created by liguohua on 2017/3/1.
 */
class O4_ImplicitDemo {

}

object O4_ImplicitDemo {
  def main(args: Array[String]) {
    implicit val name = "zhangsan"
    implicit val age = 20


    //test()//錯誤
    //test//正確,
    //test(name)//正確
    //test(implicitly)//正確
    test("xiaoqiang") //正確

    //test2()//錯誤
    //test2//正確
    //test2(name,age)//正確
    //test2(implicitly,implicitly)//正確
    //test2(implicitly,67)//正確
    //test2("huangzhong", 67) //正確


    implicit val age2 = 25
    //test2//錯誤
    //test2(implicitly,implicitly)//錯誤
    test2(implicitly,67)//正確
  }

  def test(implicit name: String): Unit = {
    println(name)
  }

  def test2(implicit name: String, age: Int): Unit = {
    println(name + "\t" + age)
  }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章