2.5.3 接口
知识点:主构造函数、多态
Student类继承Person类
Student类实现Study接口
Student的代码:
class Student(name: String,age: Int) : Person(name,age), Study {
override fun readBooks(){
println(name+" is reading")
}
override fun doHomework(){
println(name+" is doing homework")
}
}
Person的代码
open class Person(val name: String,val age:Int){
}
Study的代码
interface Study {
fun readBooks()
fun doHomework()
}
主函数的代码
fun main(){
val student=Student("Jack",19)
doStudy(student)
}
fun doStudy(study: Study){
study.readBooks()
study.doHomework()
}
最后,我成功啦。