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
}
上一篇: 堆排序原理及JAVA实现
下一篇: 对泛型的一些新的认识