伴生类和伴生对象:如果有一个class,还有一个与class同名的object,那么就称这个object是class的伴生对象,class是object的伴生类
package com.imooc.scala.course04
object ApplyApp {
def main(args: Array[String]): Unit = {
/*for (i <- 1 to 10){
ApplyTest.incr
}
println(ApplyTest.count)// 10 说明object本身是一个单例对象*/
val b = ApplyTest() // ==> Object.apply
/*结果如下:
Object ApplyTest enter...
Object ApplyTest leave...
Object ApplyTest apply...*/
val c = new ApplyTest()
println(c)
c()
/*结果:class ApplyTest apply...*/
//类名() ==> Object.apply
//对象() ==> Class.apply
}
}
class ApplyTest{ //伴生类
def apply()={
println("class ApplyTest apply...")
}
}
object ApplyTest{ //伴生对象
println("Object ApplyTest enter...")
var count = 0
def incr = {
count = count + 1
}
//最佳实践:在Object的apply方法中new Class
def apply()={
println("Object ApplyTest apply...")
new ApplyTest
}
println("Object ApplyTest leave...")
}