Scala Apply

 

class ApplyTest{

  //一定要写(),不加括号就报错。
  def apply() = println("Into Spark!")
  def havaAtry(){
    println("have a try!")
  }
}

object ApplyTest{

  //一定要写(),不加括号就报错。
  def apply() = new ApplyTest
}

//调用时:

val test = ApplyTest()

test.haveAtry()

//类名+(),相当于调用class的Apply()方法。

println(test())

//类名后面加括号,相当于调用伴生对象的apply方法

posted @ 2016-07-20 22:58  Jiang_Xi  阅读(134)  评论(0编辑  收藏  举报