Java 單例的寫法
public class Single {
private static Single sInstance = null;
private Single() {
}
public static Single getInstance() {
if (sInstance == null) {
sInstance = new Single();
}
return sInstance;
}
public void test() {
}
}
Kotlin 單例的寫法
class KotSingle private constructor() {
companion object {
val instance: KotSingle by lazy {
SingleonHolder.holder
}
}
private object SingleonHolder {
val holder = KotSingle()
}
fun test() {
}
}