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 method test apply in class test apply in class 10 */
unapplyDemo
最新推荐文章于 2021-07-09 11:36:27 发布