Kotlin 泛型

2018-08-22  本文已影响7人  wanTag

泛型使用实例:

 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
    }
上一篇下一篇

猜你喜欢

热点阅读