Kotlin面向对象编程概念全解析
1. 接口实现
在Kotlin里,类遵循单继承原则,也就是每个类只能继承一个类。不过,接口支持多重继承,这在Kotlin中被称作多重一致性。只要一个类定义了接口的所有成员,它就能够实现多个接口。
下面是一个多重接口实现的例子:
interface InterfaceProperties {
val c : Int
val d : String
get() = "Helloo"
}
interface InterfaceMethods {
fun description()
}
class MultipleInterface : InterfaceProperties, InterfaceMethods {
override val c : Int
get() = 60
override fun description()
{
println("The Multiple Interfaces implemented")
}
}
fun main()
{
val obj = MultipleInterface()
obj.description()
}
在这个例子中,程序里定义了两个接口 InterfaceProperties 和 InterfaceMethods 。类 MultipleInterface 实现了这些接口,并且在
超级会员免费看
订阅专栏 解锁全文
153

被折叠的 条评论
为什么被折叠?



