接口:代表的是事物的能力
抽象类:反映的是事物的本质
kotlin中的接口
interface IMan {
fun xiao()
}
kotlin中的抽象类
abstract class Human {
abstract fun eat()
}
kotlin中实现接口
class Man :IMan{
override fun xiao() {
println("哈哈")
}
}
kotlin中实现继承抽象类
class Man :Human(){
override fun eat() {
println("我会吃饭")
}
}
两者实现的区别是: 实现接口时 : 后面直接跟接口类名称
继承抽象类时 : 后面需要添加()
同时继承抽象类和接口
class Man :Human(),IMan{
override fun xiao() {
println("哈哈")
}
override fun eat() {
println("吃饭")
}
}
网友评论