个性化阅读
专注于IT技术分析

Kotlin接口

点击下载

本文概述

接口是类的蓝图。Kotlin接口类似于Java8。它包含抽象方法声明以及方法的实现。

定义界面

接口是使用关键字interface定义的。例如:

interface MyInterface {
val id: Int // abstract property
    fun absMethod()// abstract method
    fun doSomthing() {
      // optional body
    }
}

默认情况下, 仅在没有其方法主体的情况下声明的方法是抽象的。

为什么要使用Kotlin界面?

以下是使用界面的原因:

  • 使用接口支持多重继承的功能。
  • 它可以用来实现松散耦合。
  • 它用于实现抽象。

子类仅扩展一个超类, 但实现多个接口。父类或接口实现的扩展使用其子类中的(:)运算符完成。

实施接口

在此示例中, 我们在InterfaceImp类中实现接口MyInterface。 InterfaceImp类提供在MyInterface接口中声明的属性ID和抽象方法absMethod()的实现。

interface MyInterface  {
var id: Int            // abstract property
    fun absMethod():String    // abstract method
    fun doSomthing() {
println("MyInterface doing some work")
    }
}
class InterfaceImp : MyInterface {
    override var id: Int = 101
    override fun absMethod(): String{
return "Implementing abstract method.."
    }
}
fun main(args: Array<String>) {
val obj = InterfaceImp()
println("Calling overriding id value = ${obj.id}")
obj.doSomthing()
println(obj.absMethod())
}

输出:

Calling overriding id value = 101
MyInterface doing some work
Implementing abstract method..

实施多个接口

我们可以在同一类中实现不同接口的多个抽象方法。所有抽象方法必须在子类中实现。接口的其他非抽象方法可以从派生类中调用。

例如, 分别使用抽象方法doSomthing()和absMethod()创建两个接口MyInterface1和MyInterface2。这些抽象方法在派生类MyClass中被重写。

interface MyInterface1 {
    fun doSomthing()
}
interface MyInterface2 {
    fun absMethod()
}
class MyClass : MyInterface1, MyInterface2 {
    override fun doSomthing() {
println("overriding doSomthing() of MyInterface1")
    }

    override fun absMethod() {
println("overriding absMethod() of MyInterface2")
    }
}
fun main(args: Array<String>) {
val myClass = MyClass()
myClass.doSomthing()
myClass.absMethod()
}

输出:

overriding doSomthing() of MyInterface1
overriding absMethod() of MyInterface2

解决具有相同方法的不同接口的冲突

让我们看一个示例, 其中接口MyInterface1和接口MyInterface2都包含相同的非抽象方法。 MyClass类提供了这些接口的实现。使用MyClass的对象调用接口的方法会产生错误。

interface MyInterface1 {
    fun doSomthing(){
println("overriding doSomthing() of MyInterface1")
    }
}
interface MyInterface2 {
    fun doSomthing(){
println("overriding doSomthing() of MyInterface2")
    }
}
class MyClass : MyInterface1, MyInterface2 {

}
fun main(args: Array<String>) {
val myClass = MyClass()
myClass.doSomthing()
}

输出:

Kotlin: Class 'MyClass' must override public open fun doSomthing(): Unit defined in MyInterface1 because it 
inherits multiple interface methods of it

为了解决上述问题, 我们需要指定要调用的接口的特定方法。让我们看下面的例子。

在下面的示例中, 两个接口MyInterface1和MyInterface2分别包含两个抽象方法adsMethod()和absMethod(name:String)和非抽象方法doSomthing()。 MyClass类同时实现接口和重写抽象方法absMethod()和absMethod(name:String)。要覆盖非抽象方法doSomthing(), 我们需要使用超级关键字作为super <interface_name> .methodName()的方法指定接口名称。

interface MyInterface1 {
    fun doSomthing() {
println("MyInterface 1 doing some work")
    }
        fun absMethod()
}
interface MyInterface2 {
    fun doSomthing(){
println("MyInterface 2 doing some work")
    }
    fun absMethod(name: String)
}
class MyClass : MyInterface1, MyInterface2 {
    override fun doSomthing() {
        super<MyInterface2>.doSomthing()
    }

    override fun absMethod() {
println("Implements absMethod() of MyInterface1")
    }
    override fun absMethod(n: String) {
println("Implements absMethod(name) of MyInterface2 name is  $n")
    }
}
fun main(args: Array<String>) {
val myClass = MyClass()
myClass.doSomthing()
myClass.absMethod()
myClass.absMethod("Ashu")
}

输出:

MyInterface 2 doing some work
Implements absMethod() of MyInterface1
Implements absMethod(name) of MyInterface2 name is  Ashu
interface MyInterface1 {
    fun doSomthing() {
println("MyInterface 1 doing some work")
    }
    fun absMethod()
}

interface MyInterface2 {
    fun doSomthing() {
println("MyInterface 2 doing some work")
    }
   fun absMethod() {
println("MyInterface 2 absMethod")
    }

}

class C : MyInterface1 {
    override fun absMethod() {
println("MyInterface1 absMethod implementation")
    }
}

class D : MyInterface1, MyInterface2 {
    override fun doSomthing() {
        super<MyInterface1>.doSomthing()
        super<MyInterface2>.doSomthing()
    }

    override fun absMethod() {

        super<MyInterface2>.absMethod()
    }
}

fun main(args: Array<String>) {
val d = D()
val c = C()
d.doSomthing()
d.absMethod()
c.doSomthing()
c.absMethod()
}

输出:

MyInterface 1 doing some work
MyInterface 2 doing some work
MyInterface 2 absMethod
MyInterface 1 doing some work
MyInterface1 absMethod implementation
赞(0)
未经允许不得转载:srcmini » Kotlin接口

评论 抢沙发

评论前必须登录!