美文网首页
Kevin Learn Kotlin:类、对象和接口

Kevin Learn Kotlin:类、对象和接口

作者: Kevin_小飞象 | 来源:发表于2022-02-09 10:43 被阅读0次
1148188223.jpg

Kotlin 也是面向对象编程。面向对象的语言是可以创建类的。类就是对事物的一种封装,比如人、汽车、房子等任何事物,我们都可以将它们封装成一个类,而类中又可以拥有自己的字段和函数。

类与对象

1. 简单使用

fun main() {
    val person = Person()
    person.name = "Kevin"
    person.age = 18
    person.eat()
}

class Person {
    var name = ""
    var age = 0

    fun eat() {
        println(name + "is eating. He is " + age + "years old.")
    }
}

2. 访问性修饰符:open、final、abstract

一般类

  • 在 Kotlin 中,类和方法默认都是 final 的,如果想允许创建一个类的子类,需要使用 open 修饰符来标示这个类,此外还需要给每一个允许被重写的属性或方法添加 open 修饰符。

  • 如果重写了一个基类的成员,重写了的函数同样默认是 open 的,如果想改变这一行为,可以显示地将重写的成员标注为 final 。

抽象类
我们可以将一个类声明为 abstract ,这种类不能被实例化,一个从抽象类通常包含一些没有实现并且必须在子类重写的抽象成员:

  • 抽象类中的抽象函数:没有函数体就默认是 abstract 的,不一定要加上关键字,其访问性始终是 open 的。
  • 抽象类中的非抽象函数:默认是 final 的,如果需要重写,那么需要加上 open 修饰符。

小结
open、final 和 abstract 这三个访问修饰符都只适用于类,不能用在接口当中:

  • open:用于声明一个类可以被继承,或者方法可以被子类重写。
  • final:不允许类被继承,或者不允许方法被重写。
  • abstract:声明抽象类,或者抽象类中的抽象方法。

接口

  • 一个简单的 Kotlin 接口使用 interface 关键字来声明,所有实现这个接口的非抽象类都需要实现接口中定义的抽象方法。
  • Kotlin 在类名后面使用 冒号 代替了 Java 中的 extends 和 implements 关键字,一个类可以实现多个接口,但是只能继承一个类。
  • override 修饰符用来标注被重写的父类或者接口的方法和属性,并且是 强制要求 的。

如何在 Kotlin 中声明和使用接口?

在 Kotlin 中,我们声明接口,例如:

interface ResponseListener {
    fun onSuccess(response: Response) 

    fun onError(error: Error) 
}

在这里,我们有一个具有两个抽象函数的接口,其中 Response 和 Error 是数据类,例如:

data class Response(val data: String)
data class Error(val error: String)

现在,要使用这个接口,我们需要在类中实现它,例如:

class MyView() : ResponseListener {
    override fun onSuccess(response: Response) {

    }

    override fun onError(error: Error) {
        
    }
}

在这里,我们创建了一个 MyView 类,它实现了我们的接口,并且它的功能在类中被覆盖。

具有属性和默认实现的接口

interface ResponseListener {
    fun onSuccess(response: Response) {
        Log.d("ResponseListener", "onSuccess")
    }

    fun onError(error: Error) {
        Log.d("ResponseListener", "onError")
    }
}

我们现在将它实现到一个类,例如:

class MyView() : ResponseListener {
    override fun onSuccess(response: Response) {
        super.onSuccess(response)
    }

    override fun onError(error: Error) {
        super.onError(error)
    }
}

接口中的属性

interface SetupAddition {
    val numOne: Int
    val numTwo: Int
    fun add() = numOne + numTwo
}

在这里,我们有两个属性 numOne 和 numTwo 。它还有一个函数 add() 将返回两个数字的总和。现在,要使用它例如:

class Addition : SetupAddition {
    override val numOne: Int
        get() = 5
    override val numTwo: Int
        get() = 10

    override fun add(): Int {
        return super.add()
    }
}

使用:

Addition().add()

接口中的继承

我们可以将属性和功能从一个接口继承到另一个接口。
现有一个接口,如下:

interface Numbers {
    var numTwo: Int
    fun numOne(): Int // This is a function now
}

由另一个接口实现:

interface SetupAddition : Numbers {
    fun add() = numOne() + numTwo
}

在这里,您可以看到 SetupAddition 我们可以访问接口的函数和属性,Numbers并且可以执行对 SetupAddition 接口的功能的添加。

使用:

class Addition() : SetupAddition {
    
    override fun numOne(): Int = 5
    override var numTwo: Int = 10
    
    override fun add(): Int {
        return super.add()
    }
}

实现多个接口

在 Kotlin 中,我们可以在一个类中实现多个接口,它将覆盖所有实现的接口中的函数。例如:

interface Numbers {
    var numTwo: Int
    fun numOne(): Int //This is a function
}

interface SetupAddition : Numbers {
    fun add() = numOne() + numTwo
}
interface ResponseListener {
    fun onSuccess(response: Response) {
        Log.d("ResponseListener", "onSuccess")
    }

    fun onError(error: Error) {
        Log.d("ResponseListener", "onError")
    }
}

在这里,SetupAddition 继承了 Numbers,我们还有另一个名为 的接口ResponseListener。

class MyClass() : SetupAddition,ResponseListener {

    override fun numOne(): Int = 5
    override var numTwo: Int = 10

    override fun add(): Int {
        return super.add()
    }

    override fun onError(error: Error) {
        super.onError(error)
    }

    override fun onSuccess(response: Response) {
        super.onSuccess(response)
    }
}

相关文章

网友评论

      本文标题:Kevin Learn Kotlin:类、对象和接口

      本文链接:https://www.haomeiwen.com/subject/izhwkrtx.html