Kotlin学习笔记(四)对象

class Girl(name: String, age: Int, hobby: String): Person(name, age, hobby) //继承自父类的对象应使用父类的构造器
class Boy(name: String, age: Int, hobby: String): Person(name, age, hobby)

open class Person(var name: String, var age: Int, var hobby: String) { //抽象类
    init {
        println("新建了一个${this.javaClass.simpleName}对象,名字:$name,年龄:$age,兴趣:$hobby")
    }
}

fun main(args: Array) {
    val girl: Girl = Girl("王馨", 18, "网球")
    val boy: Boy = Boy("蓝源", 22, "保龄球")
}
Kotlin学习笔记(四)对象_第1张图片
运行结果

你可能感兴趣的:(Kotlin学习笔记(四)对象)