scala apply 测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package org.qianrushi

class ApplyTest {
def apply() = println("class ApplyTest")
def haveATry(): Unit ={
println("haveATry")
}
}

object ApplyTest {
def apply() = {
println("object ApplyTest")
new ApplyTest
}

}


object ApplyOperation {
def main(args: Array[String]): Unit = {
val a = ApplyTest()
a.haveATry
a() // 触发 class ApplyTest 的 apply 方法

}
}