kotlin多继承_Kotlin继承

kotlin多继承

Continuing with our series of Kotlin tutorials, today we’ll look into inheritance in Kotlin Classes. Let’s get started by creating a new IntelliJ IDEA Kotlin project.

继续我们的Kotlin教程系列,今天我们将研究Kotlin类中的继承。 让我们开始创建一个新的IntelliJ IDEA Kotlin项目。

Kotlin继承 (Kotlin Inheritance)

Inheritance is the concept of creating class hierarchies wherein we override properties and functions of the base class in its subclasses as per our needs.

继承是创建类层次结构的概念,其中我们根据需要在基类的子类中覆盖基类的属性和功能 。

All classes in Kotlin have a common superclass : Any.

Kotlin中的所有类都有一个共同的超类: Any

Inheritance allows code reusability. Typically the superclass is known as the base class or the parent class and the subclasses are the derived/child class.

继承允许代码可重用。 通常,超类称为基类或父类,子类为派生/子类。

Kotlin doesn’t allow a class to inherit from multiple classes because of famous diamond problem.

由于著名的钻石问题,Kotlin不允许一个类从多个类继承。

Kotlin继承示例 (Kotlin Inheritance Example)

Let’s put the above inheritance concept in our example code below.

让我们将上面的继承概念放在下面的示例代码中。

We’ve created a base class as shown below:

我们创建了一个基类,如下所示:

open class Employee(eName: String) {
    init {
        println("He/She is an employee")
    }

    open var name = eName
    var age: Int = 24
    open fun printDetails() {
        println("Employee Name is $name. No specific details are available in this function.")
    }
}<

你可能感兴趣的:(多态,golang,类,class,java)