每天学一点 Kotlin -- 类的进阶:继承

----《第一季Kotlin崛起:次世代Android开发 》学习笔记

总目录:每天学一点 Kotlin ---- 目录
上一篇:每天学一点 Kotlin -- 类的属性
下一篇:每天学一点 Kotlin -- 类的进阶:抽象类

1. 继承

1.1 对于继承,父类名称前面需要加上关键字 open,这个类叫作父类(或超类)。需要在继承父类的类(子类)后面加上冒号,把想要继承的类放在后面即可。示例如下:

open class Person5 {
    var name = ""
    var age = 0
}

class Student1 : Person5() {
    var schoolName = ""
    var studentId = 0
}

在继承的时候调用的是这些超类的默认构造器,所以我们在子类后面跟着的父类的括号中不需要加上任何构造参数,空的就行。

1.2 对子类的实例化并且对属性赋值也是非常简单的,子类也能够访问和设置其超类的属性。举个栗子:

open class Person6(name: String, age: Int, height: Int, likeFood: String, constByMonth: Int) {
    val name: String = name
    val age: Int = age
    val height: Int = height
    val likeFood: String = likeFood
    val constByMonth: Int = constByMonth

    fun printInfo() =
        println("name: " + name + " age: " + age + " height: " + height + " likeFood: " + likeFood + " constByMonth: " + constByMonth)
}

class Student2(
    name: String, age: Int, height: Int, likeFood: String, constByMonth: Int, schoolName: String
) : Person6(name, age, height, likeFood, constByMonth) {
    val schoolName: String = schoolName;
}

class Worker(
    name: String, age: Int, height: Int,
    likeFood: String, constByMonth: Int, nameOfFactory: String, salary: Int
) : Person6(name, age, height, likeFood, constByMonth) {
    val nameOfFactory: String = nameOfFactory
    val salary: Int = salary
}

可以看到,如果父类中有主构造函数,那么子类必须对父类的主构造函数参数就地初始化。也就是说,子类中的主构造函数传入的参数要父类的属性进行初始化,也对子类本身新增加的属性进行初始化。在 main 函数中调用的写法为:

fun main() {
    testStudent2()
}

fun testStudent2() {
    val student2 = Student2("xiaoming", 20, 185, "pingguo", 30, "xiaoming-school")
    student2.printInfo()
    val worker = Worker("worker", 21, 190, "shupian", 40, "worker-factory", 8000)
    worker.printInfo()
}

打印结果:

name: xiaoming age: 20 height: 185 likeFood: pingguo constByMonth: 30
name: worker age: 21 height: 190 likeFood: shupian constByMonth: 40

(1) 可以看到子类新加的成员变量没有输出,这时要打印子类中新加的变量,只要重写父类的 printInfo() 方法即可,但前提是父类的 printInfo() 方法是 open 的。因为如果不加任何修饰,Kotlin 会默认在前面添加 final,这样就是不可继承,不可覆盖重写的。
(2) 用 open 修饰父类中 printInfo() 方法,然后在子类中重写父类的方法:

fun main() {
    testStudent3()
}

fun testStudent3() {
    val student3 = Student3("xiaoming", 20, 185, "pingguo", 30, "xiaoming-school")
    student3.printInfo()
}

class Student3(name: String, age: Int, height: Int, likeFood: String, constByMonth: Int, schoolName: String) :
    Person7(name, age, height, likeFood, constByMonth) {
    val schoolName: String = schoolName;

    override fun printInfo() =
        println("name: " + name + " age: " + age + " height: " + height + " likeFood: " + likeFood + " constByMonth: " + constByMonth + " schoolName: " + schoolName)
}

open class Person7(name: String, age: Int, height: Int, likeFood: String, constByMonth: Int) {
    val name: String = name
    val age: Int = age
    val height: Int = height
    val likeFood: String = likeFood
    val constByMonth: Int = constByMonth

    open fun printInfo() =
        println("name: " + name + " age: " + age + " height: " + height + " likeFood: " + likeFood + " constByMonth: " + constByMonth)
}

打印结果为:

name: xiaoming age: 20 height: 185 likeFood: pingguo constByMonth: 30 schoolName: xiaoming-school

1.3 如果超类没有主构造函数,只有次构造函数,那么每个次构造函数必须用 super 关键词调用超类的构造函数对超类的属性进行初始化,具体的语法为 "constructor 子类(次构造函数):super(次构造函数)"。举个栗子:

fun main() {
    testStudent4()
}

fun testStudent4() {
    val student4 = Student4("st4", 20, "st4_schoolName");
    student4.printInfo()
}

open class Person8 {
    constructor(name: String, age: Int) {
        this.name = name
        this.age = age
    }

    var name: String = ""
    var age: Int = 0

    open fun printInfo() =
        println("name: " + name + " age: " + age)
}

class Student4 : Person8 {
    constructor(name: String, age: Int, schoolName: String)
            : super(name, age) {
        this.schoolName = schoolName
    }

    var schoolName: String = ""

    override fun printInfo() {
        println("name: ${name}, schoolName: ${schoolName}")
    }
}

打印结果;

name: st4, schoolName: st4_schoolName

1.4 上面的代码中,子类重写了父类的 printInfo() 方法。子类也可以重写父类中的属性,但是有一条原则:不能把父类中 var 变量重写为 val 常量,也就是:不能把父类中可读可写的属性改为可读不可写的。即覆盖属性只能扩充外部功能,不能减少。举个栗子:

// 第一种情况
open class Parent1 {
    open var str = "str"
}

class Child1 : Parent1() {
    override var str = "str_child"
}

// 第二种情况
open class Parent2 {
    open val str = "str"
}

class Child2_1 : Parent2() {
    override val str = "str_child"
}

class Child2_2 : Parent2() {
    override var str = "str_child"
}

// 第三种情况
class Child3 : Parent1() {
//    override val str = "str_child"  // 编辑器直接报红了
}

1.5 在主构造函数中覆盖属性:

open class Parent4{
    open val str: String  = "Parent4"
}
class Child4(override val str: String = "Child4"):Parent4(){}

上面的情况也是一种属性的重写,因为在写构造器参数的时候如果在参数名称前加一个 var 或 val 修饰符等于就是把它自动变为一个当前类的属性了。这里在参数名前加了 override val,也就是自动将它变为超类中相应属性的继承了。

1.6 Kotlin 中,如果一个类没有声明继承任何类,那么这个默认继承了 Any 类。Any 这个类中有 equals()、hashCode()、toString() 这些方法。

相关代码:https://gitee.com/fzq.com/test-demo

你可能感兴趣的:(每天学一点 Kotlin -- 类的进阶:继承)