File tree Expand file tree Collapse file tree 4 files changed +7
-5
lines changed
java/net/println/kt10/java
kotlin/net/println/kt10/kotlin Expand file tree Collapse file tree 4 files changed +7
-5
lines changed Original file line number Diff line number Diff line change 54
54
####[ 09 尾递归优化] ( http://v.qq.com/x/page/f0345wmuw2m.html )
55
55
尾递归,顾名思义,就是递归中调用自身的部分在函数体的最后一句。我们知道,递归调用对于栈大小的考验是非常大的,也经常会因为这个导致 StackOverflow,所以尾递归优化也是大家比较关注的一个话题。Kotlin 支持语法层面的尾递归优化,这在其他语言里面是不多见的。
56
56
57
- ####10 单例[ 预计 11.14 录制 ]
57
+ ####10 单例[ 11.20 上传中 ]
58
58
59
59
单例大家一定都不陌生,只要你动手写一个程序,就免不了要设计出一些全局存在且唯一的对象,他们就适合采用单例模式编写。在 Java 里面,单例模式的写法常见的有好几种,虽然简单却也是涉及了一些有意思的话题,那么在 Kotlin 当中我们要怎么设计单例程序呢?
60
60
Original file line number Diff line number Diff line change 6
6
public class PlainOldSingleton {
7
7
private static PlainOldSingleton INSTANCE = new PlainOldSingleton ();
8
8
9
- private PlainOldSingleton (){}
9
+ private PlainOldSingleton (){
10
+ System .out .println ("PlainOldSingleton" );
11
+ }
10
12
11
13
public static PlainOldSingleton getInstance (){
12
14
return INSTANCE ;
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ package net.println.kt10.kotlin
5
5
*/
6
6
class LazyNotThreadSafe {
7
7
companion object {
8
- val instance = lazy {
8
+ val instance by lazy( LazyThreadSafetyMode . NONE ) {
9
9
LazyNotThreadSafe ()
10
10
}
11
11
Original file line number Diff line number Diff line change @@ -5,11 +5,11 @@ package net.println.kt10.kotlin
5
5
*/
6
6
class LazyThreadSafeDoubleCheck private constructor(){
7
7
companion object {
8
- val instance = lazy(mode = LazyThreadSafetyMode .SYNCHRONIZED ){
8
+ val instance by lazy(mode = LazyThreadSafetyMode .SYNCHRONIZED ){
9
9
LazyThreadSafeDoubleCheck ()
10
10
}
11
11
12
- @Volatile var instance2: LazyThreadSafeDoubleCheck ? = null
12
+ private @Volatile var instance2: LazyThreadSafeDoubleCheck ? = null
13
13
14
14
fun getInstance () = {
15
15
if (instance2 == null ){
You can’t perform that action at this time.
0 commit comments