39 lines
501 B
Kotlin
39 lines
501 B
Kotlin
|
package Demo1
|
||
|
|
||
|
class InitAndSingleton{
|
||
|
//初始化
|
||
|
init{
|
||
|
println("init")
|
||
|
}
|
||
|
//带参构造方法
|
||
|
constructor(data:String){
|
||
|
println("data = ${data}")
|
||
|
}
|
||
|
//构造方法
|
||
|
constructor(){
|
||
|
println("not parma")
|
||
|
}
|
||
|
|
||
|
//定义静态方法
|
||
|
companion object{
|
||
|
//方法实现
|
||
|
fun function() {
|
||
|
println("is Function")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
fun main() {
|
||
|
InitAndSingleton()
|
||
|
InitAndSingleton("uname")
|
||
|
InitAndSingleton.function()
|
||
|
|
||
|
}
|
||
|
/** out **
|
||
|
init
|
||
|
not parma
|
||
|
init
|
||
|
data = uname
|
||
|
is Function
|
||
|
**/
|