📜  Kotlin 继承

📅  最后修改于: 2022-05-13 01:55:03.205000             🧑  作者: Mango

Kotlin 继承

继承是面向对象编程中比较重要的特性之一。继承使代码可重用,它允许现有类(基类)的所有功能被新类(派生类)继承。此外,派生类还可以添加一些自己的特性。

继承语法:

open class baseClass (x:Int ) {
      ..........
}
class derivedClass(x:Int) : baseClass(x) {
     ...........
}

在 Kotlin 中,默认情况下所有类都是final的。为了允许派生类从基类继承,我们必须在类前面使用open关键字。

Kotlin 从基类继承属性和方法:
当我们继承一个类时,所有的属性和函数也会被继承。我们可以在派生类中使用基类变量和函数,也可以使用派生类对象调用函数。

Kotlin
//base class
open class baseClass{
    val name = "GeeksforGeeks"
    fun A(){
        println("Base Class")
    }
}
//derived class
class derivedClass: baseClass() {
    fun B() {
        println(name)           //inherit name property
        println("Derived class") 
    }
}
fun main(args: Array) {
    val derived = derivedClass()
    derived.A()         // inheriting the  base class function
    derived.B()         // calling derived class function
}


Kotlin
//base class
open class Employee( name: String,age: Int,salary : Int) {
    init {
        println("My name is $name, $age years old and earning $salary per month. ")
    }
}
//derived class
class webDeveloper( name: String,age: Int,salary : Int): Employee(name, age,salary) {
    fun website() {
        println("I am website developer")
        println()
    }
}
//derived class
class androidDeveloper( name: String,age: Int,salary : Int): Employee(name, age,salary) {
    fun android() {
        println("I am android app developer")
        println()
    }
}
//derived class
class iosDeveloper( name: String,age: Int,salary : Int): Employee(name, age,salary) {
    fun iosapp() {
        println("I am iOS app developer")
        println()
    }
}
//main method
fun main(args: Array) {
    val wd = webDeveloper("Gennady", 25, 10000)
    wd.website()
    val ad = androidDeveloper("Gaurav", 24,12000)
    ad.android()
    val iosd = iosDeveloper("Praveen", 26,15000)
    iosd.iosapp()
}


Kotlin
//base class
open class Employee(name: String,age: Int) {
    init{
        println("Name of the Employee is $name")
        println("Age of the Employee is $age")
    }
}
// derived class
class CEO( name: String, age: Int, salary: Double): Employee(name,age) {
    init {
        println("Salary per annum is $salary crore rupees")
    }
}
fun main(args: Array) {
    CEO("Sunder Pichai", 42, 450.00)
}


Kotlin
//base class
open class Employee {
    constructor(name: String,age: Int){
            println("Name of the Employee is $name")
            println("Age of the Employee is $age")
    }
}
// derived class
class CEO : Employee{
    constructor( name: String,age: Int, salary: Double): super(name,age) {
        println("Salary per annum is $salary million dollars")
    }
}
fun main(args: Array) {
    CEO("Satya Nadela", 48, 250.00)
}


Kotlin
// base class
open class Animal {
    open fun run() {
        println("Animals can run")
    }
}
// derived class
class Tiger: Animal() {
    override fun run() {       // overrides the run method of base class
        println("Tiger can run very fast")
    }
}
fun main(args: Array) {
    val t = Tiger()
    t.run()
}


Kotlin
// base class
open class Animal {
    open var name: String = "Dog"
    open var speed = "40 km/hr"
 
}
// derived class
class Tiger: Animal() {
    override var name = "Tiger"
    override var speed = "100 km/hr"
}
fun main(args: Array) {
    val t = Tiger()
    println(t.name+" can run at speed "+t.speed)
}


Kotlin
// base class
open class Phone() {
    var color = "Rose Gold"
    fun displayCompany(name:String) {
        println("Company is: $name")
    }
}
// derived class
class iphone: Phone() {
    fun displayColor(){
       
        // calling the base class property color
        println("Color is: "+super.color)
           
        // calling the base class member function
        super.displayCompany("Apple")
    }
}
fun main(args: Array) {
    val p = iphone()
    p.displayColor()
}


输出:

Base Class
GeeksforGeeks
Derived class

解释:
在这里,我们有一个类和一个派生类。我们在实例化派生类的同时创建一个对象,然后用它来调用类和派生类的函数。派生的.A() 用于调用打印“基类”的函数A()derived.B()用于调用函数B() ,该函数打印从基类继承的变量并打印“Derived class”

继承的使用:

假设公司中存在三种类型的员工: webDeveloperiOSDeveloperandroidDeveloper 。他们都有一些共同的特征,比如名字、年龄,还有一些特殊的技能。
首先,我们单独创建三班,所有员工都有一些共同的和特定的技能。

在这里,他们都有一些名字年龄,但三位开发人员的开发技能各不相同。在每个班级中,我们将为每个字符复制相同的姓名年龄代码。
如果我们想添加一个薪水()功能,那么我们需要在所有三个类中复制相同的代码。这会在我们的程序中创建许多重复的代码副本,并且可能会导致更复杂和不稳定的代码。
通过使用继承,任务变得更容易。我们可以创建一个新的基类Employee,它包含 3 个原始类的共同特征。然后这三个类可以继承基类的共同特性,并可以添加一些自己的特殊特性。我们可以轻松地在Employee类中添加薪水功能而无需重复副本。

在这里,对于webDeveloper ,我们继承了基类的所有特性以及类中它自己的特性 website()。同样,我们可以对其他两个类androidDeveloperiosDeveloper执行相同的操作。它使我们的代码更易于理解和扩展。

Kotlin 继承程序——

科特林

//base class
open class Employee( name: String,age: Int,salary : Int) {
    init {
        println("My name is $name, $age years old and earning $salary per month. ")
    }
}
//derived class
class webDeveloper( name: String,age: Int,salary : Int): Employee(name, age,salary) {
    fun website() {
        println("I am website developer")
        println()
    }
}
//derived class
class androidDeveloper( name: String,age: Int,salary : Int): Employee(name, age,salary) {
    fun android() {
        println("I am android app developer")
        println()
    }
}
//derived class
class iosDeveloper( name: String,age: Int,salary : Int): Employee(name, age,salary) {
    fun iosapp() {
        println("I am iOS app developer")
        println()
    }
}
//main method
fun main(args: Array) {
    val wd = webDeveloper("Gennady", 25, 10000)
    wd.website()
    val ad = androidDeveloper("Gaurav", 24,12000)
    ad.android()
    val iosd = iosDeveloper("Praveen", 26,15000)
    iosd.iosapp()
}

输出:

My name is Gennady, 25 years old and earning 10000 per month. 
I am website developer

My name is Gaurav, 24 years old and earning 12000 per month. 
I am android app developer

My name is Praveen, 26 years old and earning 15000 per month. 
I am iOS app developer

解释:
在这里,我们有一个Employee,前缀为open关键字,其中包含派生类的公共属性Employee类有一个构造函数,其中包含三个变量“姓名、年龄薪水” 。还有三个派生webDeveloperandroidDeveloperiosDeveloper也包含主构造函数并且它们都具有三个变量。
首先,我们为webDeveloper实例化一个对象,并将姓名、年龄薪水作为参数传递给派生类。它将初始化局部变量并将这些值传递类。然后,我们使用对象“ wd”调用成员函数website() ,该对象将字符串打印到标准输出。
同样,我们为其他两个类创建对象并调用它们各自的成员函数。

Kotlin 继承主构造函数:

如果派生类包含主构造函数,那么我们需要使用派生类的参数初始化基类构造函数。在下面的程序中,我们在基类的主构造函数中有两个参数,在派生类中有三个参数。

Kotlin 程序 –

科特林

//base class
open class Employee(name: String,age: Int) {
    init{
        println("Name of the Employee is $name")
        println("Age of the Employee is $age")
    }
}
// derived class
class CEO( name: String, age: Int, salary: Double): Employee(name,age) {
    init {
        println("Salary per annum is $salary crore rupees")
    }
}
fun main(args: Array) {
    CEO("Sunder Pichai", 42, 450.00)
}

输出:

Name of the Employee is Sunder Pichai
Age of the Employee is 42
Salary per annum is 450.0 crore rupees

解释:
在这里,我们实例化派生类 CEO 并传递参数值名称、年龄和薪水。派生类局部变量使用各自的值进行初始化,并将变量名称和年龄作为参数传递给 Employee 类。
雇员类将变量的名称和值打印到标准输出并将控制权转移回派生类。然后,派生类执行 println() 语句并退出。

Kotlin 继承二级构造函数:

如果派生类不包含主构造函数,我们需要使用super关键字从派生类的二级构造函数中调用基类二级构造函数。我们还需要使用派生类的参数来初始化基类二级构造函数。

Kotlin 程序:

科特林

//base class
open class Employee {
    constructor(name: String,age: Int){
            println("Name of the Employee is $name")
            println("Age of the Employee is $age")
    }
}
// derived class
class CEO : Employee{
    constructor( name: String,age: Int, salary: Double): super(name,age) {
        println("Salary per annum is $salary million dollars")
    }
}
fun main(args: Array) {
    CEO("Satya Nadela", 48, 250.00)
}

输出:

Name of the Employee is Satya Nadela
Age of the Employee is 48
Salary per annum is 250.0 million dollars

解释:
在这里,我们实例化 CEO 类并将参数值传递给辅助构造函数。它将初始化局部变量并使用 super(name,age) 传递给基类 Employee。

覆盖成员函数和属性:

如果基类和派生类包含同名的成员函数,那么我们可以在派生类中使用override关键字覆盖基类成员函数,同时还需要用open关键字标记基类的成员函数。

覆盖成员函数的 Kotlin 程序:

科特林

// base class
open class Animal {
    open fun run() {
        println("Animals can run")
    }
}
// derived class
class Tiger: Animal() {
    override fun run() {       // overrides the run method of base class
        println("Tiger can run very fast")
    }
}
fun main(args: Array) {
    val t = Tiger()
    t.run()
}

输出:

Tiger can run very fast

同样,我们可以在派生类中重写基类的属性。

覆盖成员属性的 Kotlin 程序:

科特林

// base class
open class Animal {
    open var name: String = "Dog"
    open var speed = "40 km/hr"
 
}
// derived class
class Tiger: Animal() {
    override var name = "Tiger"
    override var speed = "100 km/hr"
}
fun main(args: Array) {
    val t = Tiger()
    println(t.name+" can run at speed "+t.speed)
}

输出:

Tiger can run at speed 100 km/hr

调用超类实现:

我们还可以使用super关键字从派生类调用基类成员函数或属性。在下面的程序中,我们使用 super 关键字在派生类中调用了基类属性color和函数displayCompany()

科特林

// base class
open class Phone() {
    var color = "Rose Gold"
    fun displayCompany(name:String) {
        println("Company is: $name")
    }
}
// derived class
class iphone: Phone() {
    fun displayColor(){
       
        // calling the base class property color
        println("Color is: "+super.color)
           
        // calling the base class member function
        super.displayCompany("Apple")
    }
}
fun main(args: Array) {
    val p = iphone()
    p.displayColor()
}

输出:

Color is: Rose Gold
Company is: Apple