package cn.itcast
/**
* Created by lishas on 2016/8/22.
*/
object ApplyDemo {
def apply(): Unit ={
println("apply invoked")
}
def apply(name:String):String={
name
}
def apply(name:Int):Int={
println("Int")
name
}
def main(args: Array[String]) {
//val a=ApplyDemo("tom")
val a=ApplyDemo(10)
println(a)
val arr=Array(1,2,3,4,5,6,7,8,9)
}
}
当获取伴生对象,Array不加括号,加括号就是调用apply方法
时间: 2024-10-11 03:51:23