Skip to content

Instantly share code, notes, and snippets.

@mio4kon
Last active August 17, 2016 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mio4kon/13783f46e27fb8e16edf642c7cdbb782 to your computer and use it in GitHub Desktop.
Save mio4kon/13783f46e27fb8e16edf642c7cdbb782 to your computer and use it in GitHub Desktop.
/**
* Created by mio4kon on 16/8/16.
* companion object 与 init 调用顺序
*/
class CompanionObj {
//构造CompanionObj才打印了
init {
println("init 构造")
}
//普通
fun a(){
println("a function ")
}
object ob{
//静态方法
fun b(){
println("b function")
}
//只有调ob的时候才打印
init {
println("init object")
}
}
companion object{
//静态方法
fun c(){
println("c function")
}
//调用CompanionObj就打印了,类似静态代码块
init {
println("init companion object")
}
}
}
fun main(args: Array<String>) {
CompanionObj().a()
CompanionObj.ob.b()
CompanionObj.c()
/*******Log******
init companion object
init 构造
a function
init object
b function
c function
*******Log******/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment