unapplyDemo
来源:互联网 发布:河北经济网络频道 编辑:程序博客网 时间:2024/06/06 12:59
class ApplyDemo { def apply() = "apply in class" def test { println("test") }}/** * 伴生对象,相当于类的静态方法 */object ApplyDemo { def stat { println("static method") } def apply() = new ApplyDemo var count = 0 def incc = { count += 1 }}object ApplyDemoTest extends App { ApplyDemo.stat //类名后面加括号,相当于调用伴生对象的apply方法 val a = ApplyDemo() a.test //对象加括号相当于调用对象的apply方法 println(a()) val b = ApplyDemo.apply() b.test println(a.apply()) for (i <- 0 until 10) { ApplyDemo.incc } println(ApplyDemo.count)}/** * static methodtestapply in classtestapply in class10 */
阅读全文
0 0