📜  Kotlin 抽象类

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

Kotlin 抽象类

在 Kotlin 中,抽象类是在 class 前面使用abstract关键字声明的。抽象类不能实例化意味着我们不能为抽象类创建对象。

抽象类声明:

abstract class className {
    .........
} 

要记住的要点:

  1. 我们不能为抽象类创建对象。
  2. 默认情况下,抽象类的所有变量(属性)和成员函数都是非抽象的。因此,如果我们想在子类中覆盖这些成员,那么我们需要使用open关键字。
  3. 如果我们将成员函数声明为抽象,那么我们不需要使用open关键字进行注释,因为它们默认是打开的。
  4. 抽象成员函数没有主体,必须在派生类中实现。

抽象类可以包含抽象和非抽象成员,如下所示:

abstract class className(val x: String) {   // Non-Abstract Property
         
    abstract var y: Int      // Abstract Property

    abstract fun method1()   // Abstract Methods

    fun method2() {          // Non-Abstract Method
        println("Non abstract function")
    }
}

Kotlin 程序在抽象类中同时使用抽象和非抽象成员——

Kotlin
//abstract class
abstract class Employee(val name: String,val experience: Int) {   // Non-Abstract
                                                                  // Property
    // Abstract Property (Must be overridden by Subclasses)
    abstract var salary: Double
      
    // Abstract Methods (Must be implemented by Subclasses)
    abstract fun dateOfBirth(date:String)
  
    // Non-Abstract Method
    fun employeeDetails() {
        println("Name of the employee: $name")
        println("Experience in years: $experience")
        println("Annual Salary: $salary")
    }
}
// derived class
class Engineer(name: String,experience: Int) : Employee(name,experience) {
    override var salary = 500000.00
    override fun dateOfBirth(date:String){
        println("Date of Birth is: $date")
    }
}
fun main(args: Array) {
    val eng = Engineer("Praveen",2)
    eng.employeeDetails()
    eng.dateOfBirth("02 December 1994")
}


Kotlin
open class Livingthings {
    open fun breathe() {
        println("All living things breathe")
    }
}
abstract class Animal : Livingthings() {
    override abstract fun breathe()
}
class Dog: Animal(){
    override fun breathe() {
        println("Dog can also breathe")
    }
}
fun main(args: Array){
    val lt = Livingthings()
    lt.breathe()
    val d = Dog()
    d.breathe()
}


Kotlin
// abstract class
abstract class Calculator {
    abstract fun cal(x: Int, y: Int) : Int
}
// addition of two numbers
class Add : Calculator() {
    override fun cal(x: Int, y: Int): Int {
        return x + y
    }
}
// subtraction of two numbers
class Sub : Calculator() {
    override fun cal(x: Int, y: Int): Int {
        return x - y
    }
}
// multiplication of two numbers
class Mul : Calculator() {
    override fun cal(x: Int, y: Int): Int {
        return x * y
    }
}
fun main(args: Array) {
    var add: Calculator = Add()
    var x1 = add.cal(4, 6)
    println("Addition of two numbers $x1")
    var sub: Calculator = Sub()
    var x2 = sub.cal(10,6)
    println("Subtraction of two numbers $x2")
    var mul: Calculator = Mul()
    var x3 = mul.cal(20,6)
    println("Multiplication of two numbers $x3")
}


输出:

Name of the employee: Praveen
Experience in years: 2
Annual Salary: 500000.0
Date of Birth is: 02 December 1994

解释:
在上述程序中, Engineer类派生自Employee类。为 Engineer 类实例化了一个对象eng 。我们在创建主构造函数时已将两个参数传递给它。这会初始化 Employee 类的非抽象属性nameexperience 。这

然后使用eng对象调用employeeDetails()方法。它将打印员工的姓名、经验和覆盖的工资值。

最后,使用eng对象调用dateOfBirth()并将参数 date 传递给主构造函数。它覆盖了 Employee 类的抽象乐趣,并将作为参数传递的值打印到标准输出。

用一个抽象的开放成员覆盖一个非抽象的开放成员——

在 Kotlin 中,我们可以使用 override 关键字后跟抽象类中的抽象来覆盖开放类的非抽象开放成员函数。在下面的程序中,我们将这样做。

通过抽象类覆盖非抽象开放函数的 Kotlin 程序 -

科特林

open class Livingthings {
    open fun breathe() {
        println("All living things breathe")
    }
}
abstract class Animal : Livingthings() {
    override abstract fun breathe()
}
class Dog: Animal(){
    override fun breathe() {
        println("Dog can also breathe")
    }
}
fun main(args: Array){
    val lt = Livingthings()
    lt.breathe()
    val d = Dog()
    d.breathe()
}

输出:

All living things breathe
Dog can also breathe

多个派生类——

抽象类的抽象成员可以在所有派生类中被覆盖。在程序中,我们重写了计算器的三个派生类中的cal函数。

在多个派生类中覆盖抽象函数的 Kotlin 程序 -

科特林

// abstract class
abstract class Calculator {
    abstract fun cal(x: Int, y: Int) : Int
}
// addition of two numbers
class Add : Calculator() {
    override fun cal(x: Int, y: Int): Int {
        return x + y
    }
}
// subtraction of two numbers
class Sub : Calculator() {
    override fun cal(x: Int, y: Int): Int {
        return x - y
    }
}
// multiplication of two numbers
class Mul : Calculator() {
    override fun cal(x: Int, y: Int): Int {
        return x * y
    }
}
fun main(args: Array) {
    var add: Calculator = Add()
    var x1 = add.cal(4, 6)
    println("Addition of two numbers $x1")
    var sub: Calculator = Sub()
    var x2 = sub.cal(10,6)
    println("Subtraction of two numbers $x2")
    var mul: Calculator = Mul()
    var x3 = mul.cal(20,6)
    println("Multiplication of two numbers $x3")
}

输出:

Addition of two numbers 10
Subtraction of two numbers 4
Multiplication of two numbers 120
Division of two numbers 3