美文网首页
Kotlin 泛型

Kotlin 泛型

作者: wanTag | 来源:发表于2018-08-22 17:24 被阅读7次

    泛型使用实例:

     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
        }
    

    相关文章

      网友评论

          本文标题:Kotlin 泛型

          本文链接:https://www.haomeiwen.com/subject/ucyyiftx.html