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 }
相关推荐
Kapaseker10 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
A0微声z2 天前
Kotlin Multiplatform (KMP) 中使用 Protobuf
kotlin
alexhilton3 天前
使用FunctionGemma进行设备端函数调用
android·kotlin·android jetpack
lhDream3 天前
Kotlin 开发者必看!JetBrains 开源 LLM 框架 Koog 快速上手指南(含示例)
kotlin
RdoZam3 天前
Android-封装基类Activity\Fragment,从0到1记录
android·kotlin
Kapaseker4 天前
研究表明,开发者对Kotlin集合的了解不到 20%
android·kotlin
糖猫猫cc4 天前
Kite:两种方式实现动态表名
java·kotlin·orm·kite
如此风景4 天前
kotlin协程学习小计
android·kotlin
Kapaseker5 天前
你搞得懂这 15 个 Android 架构问题吗
android·kotlin
zh_xuan5 天前
kotlin 高阶函数用法
开发语言·kotlin