写的最简单的情况下的继承。
/**
* 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.