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 f
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值