class ApplyTest {
def apply() = println("I am into Spark so much!!!.....class....")
def haveATry {
println("Have a try on apply!")
}
}
object ApplyTest {
def apply() = {
println("I am into Scala so much!!!.........object.......")
new ApplyTest // 返回的是类的实例
}
}
object ApplyOperation {
def main(args: Array[String]) {
val array = Array(1, 2, 3, 4, 5)
val a = ApplyTest() // 这是创建一个对象 ,直接调用该对象中的apply()方法
a.haveATry //
// val a = new ApplyTest // 这是实例化一个类
// a.haveATry
println(a()) // 就调用了ApplyTest类中apply方法
//
}
学习笔记11:Scala中伴生类和伴生对象中Apply方法的应用
最新推荐文章于 2020-09-14 22:44:55 发布