Scala中的Apply

/**
 * Created by Administrator on 2014-12-31.
 */
class ApplyTest {
  def apply() = "Apply customer"

  def test(): Unit = {
    println("test")
  }
}

object ApplyTest{
  def apply() = new ApplyTest

  def static: Unit ={
    println("i‘m a static method")
  }
}


object Apply {
  def main(args: Array[String]) {
    val app = ApplyTest()  //这里使用的是object AppyTest  , 因为apply中实例化了class AppleTest,所以才能调用test
    app.test

    val app1 = new ApplyTest  //这里实例化的是class ApplyTest
    app1.test

    println(app())
    println(app1())
  }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。