Kotlin 类的成员

2018-08-23  本文已影响5人  wanTag

实例:
WildAnimal.kt

class WildAnimal(var name: String, val sex: Int = 0) {
    var sexName: String = ""

    init {
        sexName = if (sex == 0) "公" else "母"
    }

    fun getDesc(tag: String): String {
        return "欢迎来到${tag},这是${name}是${sexName}的"
    }
}

调用

 private var cCount = 0

 @SuppressLint("SetTextI18n")
private fun getDesc() {
    btn_desc.setOnClickListener { v ->
        var animal = when (cCount % 2) {
            0 -> WildAnimal(animalName)
            else -> WildAnimal(animalName, animalSex)
        }
    /**
    text_class_simple.text = "这是${animal.name}是${animal.sexName}的"**/
    text_class_simple.text = animal.getDesc("动物园")

    }
}

上一篇下一篇

猜你喜欢

热点阅读