kotlin的abstract抽象类与interface接口建模按钮android点击事件处理
例如:
abstract class View {
private var listener: OnClickListener? = null
private var enabled: Boolean = false
constructor() {
println("view constructor")
}
fun setClickListener(l: OnClickListener) {
listener = l
}
fun setEnable(bool: Boolean) {
enabled = bool
}
fun performClick() {
println("view performClick")
if (enabled) {
listener?.onClick(this@View) //也可以this,this@后面指定是谁
}
}
}
interface OnClickListener {
fun onClick(view: View)
}
class Button : View()
fun main(args: Array<String>) {
var button = Button()
button.setEnable(true)
button.setClickListener(object : OnClickListener {
override fun onClick(view: View) {
println("on click")
}
})
button.performClick()
}
输出:
view constructor
view performClick
on click
https://blog.csdn.net/zhangphil/category_12220817.htmlhttps://blog.csdn.net/zhangphil/category_12220817.html