先声明一个接口
interface TestInterface {
fun toAge()
fun toName()
}
在kotlin中继承和实现接口都是用":"冒号来实现 用"," 逗号分开
现在声明一个Student类 来实现一个上面定义的接口
class Study(name: String, age: Int) : Person(name, age), TestInterface {
override fun toAge() {
println("Study to toAge")
}
override fun toName() {
println("Study to toName")
}
}
main方法中调用

查看控制台

简单的接口就是这样了
现在给toName方法加上方法体
interface TestInterface {
fun toAge()
fun toName(){
println("output function body")
}
}
可以看到Study这里并没有报错

最低0.47元/天 解锁文章
474

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



