欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kotlin 泛型

程序员文章站 2022-03-12 23:00:43
...

泛型使用实例:

 private var cCount = 0

btn_count1.setOnClickListener { v ->
    cCount = 0
    showResult()
}
btn_count2.setOnClickListener { v ->
    cCount = 1
    showResult()
}

btn_count3.setOnClickListener { v ->
    cCount = 2
    showResult()
}

   private fun showResult() {
        text_show.text = when (cCount % 3) {
            0 -> appendString<String>("古代四大发明", "造纸", "印刷", "火药", "指南针")
            1 -> appendString<Int>("小于10的数", 2, 3, 5, 7)
            else -> appendString<Double>("烧钱的日子", 5.20, 6.18, 11.11, 12.12)
        }
    }

    fun <T> appendString(tag: String, vararg otherInfo: T?): String {
        var str: String = "$tag:"
        for (item in otherInfo) {
            str = "$str${item.toString()} , "
        }
        return str
    }