1、定义两个接口,其中有一个同名的函数showOff
interface Clickable {
fun click();
fun showOff() = println("I'm clickable!")
}
interface Focusable {
fun setFocus(b: Boolean) = println("I ${if (b) "get" else "lost"}")
fun showOff() = println("I'm focuable")
}
2、子类在实现时,必须实现同名的函数
class Button : Clickable, Focusable {
override fun click() {
println("I was clicked")
}
override fun showOff() {
super<Clickable>.showOff()
super<Focusable>.showOff()
}
}
3、抽象类中函数的可见性
abstract class Animated {
abstract fun animate() //抽象方法必须由子类进行重写
open fun stopAnimating(){}
fun animateTwice(){} //抽象类中的非抽象函数,并不是默认open的
}