Last active
August 17, 2016 07:57
-
-
Save mio4kon/13783f46e27fb8e16edf642c7cdbb782 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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