Kotlin 基础用法

1:单例模式

2:循环

===================【单例1】===================

======【单例1

class SingleInstanceOne private constructor() {

    private object SingleHolder {
        val INSTANCE = SingleInstanceOne()
    }

    companion object {
        fun getInstance() =
            SingleHolder.INSTANCE
    }

}

=====【单例2

class SingleInstanceTwo private constructor() {

    companion object {
        val INSTANCE: SingleInstanceTwo by lazy {
            SingleInstanceTwo()
        }
    }

}

=====【单例3  带参数

class SingleInstanceThere private constructor(context: Context) {

    init {
        // do init
    }

    open class SingletonHolder<out T : Any, in A>(private val creator: (A) -> T) {

        private var instance: T? = null

        fun getInstance(arg: A): T =
            instance ?: synchronized(this) {
                instance ?: creator(arg).apply {
                    instance = this
                }
            }
    }

    companion object : SingletonHolder<SingleInstanceThere, Context>(::SingleInstanceThere)

    fun doSomething() {

    }
}

===================【循环】===================

======【循环1

        // 打印:1 到 10
        for (index in 1..10) {
            Timber.tag(TAG).d("test1() $index")
        }

======【循环2

        // 打印:1 到 9
        for (index in 1 until 10) {
            Timber.tag(TAG).d("test1() $index")
        }

======【循环3

        // 倒序 输出 10,9,8...1
        for (index in 10 downTo 1) {
            Timber.tag(TAG).d("$index")
        }

======【循环4

        // 输出 1.3.5.7.9
        for (index in 1..10 step 2) {
            Timber.tag(TAG).d("$index")
        }
        // 输出 1.4.7.10
        for (index in 1..10 step 3) {
            Timber.tag(TAG).d("$index")
        }

======【循环5

    private val list = arrayListOf<String>("0", "1", "2", "3", "4")
    private fun test5() {
        // 输出 0 1 3
        for ((index, item) in list.withIndex()) {
            if (index == 2) continue
            if (index == 4) break
            Timber.tag(TAG).d("$item")
        }
    }

======【循环6

         val list = arrayListOf<String>("0", "1", "2", "3", "4")   

        // 输出 0 1
        loop@ for ((index, item) in list.withIndex()) {
            if (index == 2) {
                break@loop
            }
            Timber.tag(TAG).d("$item")
        }

        // 输出 1 11 12
        loop@ for (index in 1..10) {
            Timber.tag(TAG).d("$index")
            for (item in 11..20) {
                if (item == 13) {
                    break@loop
                }
                Timber.tag(TAG).d("$item")
            }
        }

======【循环7

    private val list = arrayListOf<String>("0", "1", "2", "3", "4")
    private fun test7() {
        // 输出 0 1 3 4
        list.forEach list@{
            if (it == "2") {
                return@list
            }
            Timber.tag(TAG).d("$it")
        }
    }

======【循环8

    private val list = arrayListOf<String>("0", "1", "2", "3", "4")
    private fun test8() {
        // 输出 0 1 3 4
        list.forEachIndexed { index, s ->
            if (index == 2) return@forEachIndexed
            Timber.tag(TAG).d("$s")
        }
        // 输出 0 1
        run outSide@{
            list.forEachIndexed { index, s ->
                if (index == 2) return@outSide
                Timber.tag(TAG).d("$s")
            }
        }
    }

===================【回调】===================

    // inline是修饰内联方法,有lambda 参数都应该加一下内联方法修饰,提高效率
    private inline fun request(call: () -> Unit) {

    }

    private inline fun request2(call: (code: Int, msg: String) -> Unit) {

    }

    private inline fun request3(par: String, call: (code: Int, msg: String) -> Unit) {

    }

    //使用
    request {

    }

    request2 { code, msg ->

    }

    request3("") { code: Int, msg: String ->

    }


    


猜你喜欢

转载自blog.csdn.net/Leo_Liang_jie/article/details/117928997
今日推荐