写的最简单的情况下的继承。
/**
* You can edit, run, and share this code.
* play.kotlinlang.org
*/
fun main() {
println("Hello, world!!!")
val stu=Student()
println("student number is:"+stu.sno)
println("student grade is:"+stu.grade)
println("student age is:"+stu.age)
stu.eat()
}
class Student:Person(){
var sno="007"
var grade=0
}
open class Person{//使用open 关键字来表示一个类是可以继承的,否则不加的话,类都是final的,不可以被继承
var name="Tom"
var age=0
fun eat(){
println(name+" is eating.He is "+age+" years old.")
}
}
运行结果:
Hello, world!!!
student number is:007
student grade is:0
student age is:0
Tom is eating.He is 0 years old.
本文介绍了在Kotlin中如何创建一个简单的继承关系,通过`Student`类继承自`Person`类,并展示实例化和调用父类方法的过程。
7971

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



