scala_apply函数

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
27
28
29
30
class ApplyOperation{

}

class ApplyTest{
def apply() = println("I am into spark so much!")
def haveTry: Unit ={
println("have a try on apply")
}
}

object ApplyTest{
def apply() ={
println("I am into Scala so much")

new ApplyTest
}
}

object ApplyOperation{
def main(args : Array[String]){
val array = Array(1,2,3,4)

val a = ApplyTest()// 调用object 的apply

a.haveTry

a() // class apply use
}
}

输出
I am into Scala so much
have a try on apply
I am into Spark so much!!!

参考
scala apply方法 笔记
动手实战Scala中的apply方法和单例对象

scala case class apply

Scala会给case类自动添加一个单例对象

参考探索Scala(4)– Case Classes

欢迎关注我的公众号:沉迷Spring
显示 Gitment 评论
0%