Scala中的Apply

文章来自:http://www.cnblogs.com/hark0623/p/4194940.html  转载请注明

/**
* 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()) } }
原文地址:https://www.cnblogs.com/hark0623/p/4194940.html