Kotlin 接口

Kotlin 的接口可以既包含抽象方法的声明也包含实现;接口无法保存状态;可以有属性但必须声明为抽象或提供访问器实现

1、定义

使用关键字 interface 来定义接口

Kotlin 复制代码
interface MyInterface {
    fun bar()
    fun foo() {
      // 可选的方法体
    }
}
2、 实现接口

一个类或者对象可以实现一个或多个接口

Kotlin 复制代码
class Child : MyInterface {
    override fun bar() {
        // 方法体
    }
}
3、接口中的属性

可在接口中定义属性,该属性要么是抽象的,要么提供访问器的实现,且不能有幕后字段(backing field)

Kotlin 复制代码
interface MyInterface {
    val prop: Int // 抽象的

    val propertyWithImplementation: String
        get() = "foo"

    fun foo() {
        print(prop)
    }
}

class Child : MyInterface {
    override val prop: Int = 29
}
4、接口继承

一个接口可以从其他接口派生,意味着既能提供基类型成员的实现也能声明新的函数与属性;实现这样接口的类只需定义所缺少的实现

Kotlin 复制代码
interface Named {
    val name: String
}

interface Person : Named {
    val firstName: String
    val lastName: String

    override val name: String get() = "$firstName $lastName"
}

data class Employee(
    // 不必实现"name"
    override val firstName: String,
    override val lastName: String,
    val position: Position
) : Person
5、覆盖冲突解决

实现多个接口时,可能会遇到同一方法继承多个实现的问题,需要实现从多个接口继承的所有方法,并指明继承类如何实现它们

Kotlin 复制代码
interface A {
    fun foo() { print("A") }
    fun bar()                  // 在接口中没有方法体时默认为抽象
}

interface B {
    fun foo() { print("B") }
    fun bar() { print("bar") }
}

class C : A {
    override fun bar() { print("bar") } // 必须重写A的bar()
}

// 需要实现多个接口继承的所有方法 foo() bar()
class D : A, B {
    override fun foo() {
        super<A>.foo()
        super<B>.foo()
    }

    override fun bar() {
        super<B>.bar()
    }
}
6、函数式接口

只有一个抽象方法的接口或 单一抽象方法的 接口,可以有多个非抽象成员,但只能有一个抽象成员

使用 fun 修饰符声明一个函数式接口

Kotlin 复制代码
fun interface KRunnable {
   fun invoke()
}

SAM 转换

对于函数式接口,可以通过 lambda表达式实现 SAM 转换,从而使代码更简洁、更有可读性

Kotlin 复制代码
fun interface IntPredicate {
   fun accept(i: Int): Boolean
}
// 不使用 SAM 转换
// 创建一个类的实例
val isEven = object : IntPredicate {
   override fun accept(i: Int): Boolean {
       return i % 2 == 0
   }
}
// 利用 Kotlin 的 SAM 转换
// 通过 lambda 表达式创建一个实例
val isEven = IntPredicate { it % 2 == 0 }
相关推荐
太过平凡的小蚂蚁6 小时前
Kotlin 协程中常见的异步返回与控制方式(速览)
开发语言·前端·kotlin
雨白10 小时前
Kotlin Flow 入门:构建响应式异步数据流
android·kotlin
shayudiandian11 小时前
【Kotlin】数组集合常用扩展函数
kotlin
I'm Jie1 天前
(五)Gradle 依赖传递与冲突处理
java·spring boot·spring·kotlin·gradle·maven
消失的旧时光-19431 天前
Kotlin 高阶函数在回调设计中的最佳实践
android·开发语言·kotlin
用户092 天前
Kotlin Flow的6个必知高阶技巧
android·面试·kotlin
用户092 天前
Jetpack Compose静态与动态CompositionLocal深度解析
android·面试·kotlin
Jeled2 天前
「高级 Android 架构师成长路线」的第 1 阶段 —— 强化体系与架构思维(Clean Architecture 实战)
android·kotlin·android studio·1024程序员节
明道源码2 天前
Kotlin 控制流、函数、Lambda、高阶函数
android·开发语言·kotlin
橙子199110162 天前
在 Kotlin 中,ViewModel 的获取
开发语言·vue.js·kotlin