package
com.jn.scala.oop
/**
* @author jiangning
*/
class
ApplyTest{
def
apply
() =
println
(
"I am student"
)
def
haveATry
{
println
(
"have a try"
)
}
}
object
ApplyTest
{
def
apply
() = {
//类的伴生对象生成类的实例对象
println
(
"I am a tearch"
)
new
ApplyTest
}
}
object
ApplyOperation
{
def
main
(
args
: Array[
String
]){
val
arr
=
Array
(
1
,
2
,
3
,
4
,
5
)
//集合嵌套集合不用反复用new
// 1.利用伴生对象进行调用
// val a = ApplyTest()//这里直接用的object ApplyTest,执行 new ApplyTest为返回值,所以就可以调用haveATry
// a.haveATry
// 2.在类中调用apply方法
val
b
=
new
ApplyTest
b
.
haveATry
println
(
b
())
//类对象的实例对象加一个括号就可以调用类中apply方法,不加括号就是对象的地址。
}
}