Kotlin - 接口

Kotlin - 接口 首页 / Kotlin入门教程 / Kotlin - 接口

接口是类的蓝图。Kotlin接口类似于Java8

接口声明

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

interface MyInterface {
    val id: Int//抽象属性
    fun absMethod()// 抽象方法
    fun doSomthing() {
     //可选方法体
    }
}

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

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

    接口实例

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

    interface MyInterface  {
        var id: Int           //抽象属性
        fun absMethod():String   //抽象方法
        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的对象调用接口的方法会产生错误。

    链接:https://www.learnfk.comhttps://www.learnfk.com/kotlin/kotlin-interface.html

    来源:LearnFk无涯教程网

    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()
    }

    输出:

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

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

    在下面的示例中,两个接口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
    

    祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

    技术教程推荐

    推荐系统三十六式 -〔刑无刀〕

    持续交付36讲 -〔王潇俊〕

    Web协议详解与抓包实战 -〔陶辉〕

    OpenResty从入门到实战 -〔温铭〕

    ZooKeeper实战与源码剖析 -〔么敬国〕

    MySQL 必知必会 -〔朱晓峰〕

    徐昊 · TDD项目实战70讲 -〔徐昊〕

    超级访谈:对话玉伯 -〔玉伯〕

    AI大模型企业应用实战 -〔蔡超〕

    好记忆不如烂笔头。留下您的足迹吧 :)