# Kotlin 接口 > 原文: [https://www.programiz.com/kotlin-programming/interfaces](https://www.programiz.com/kotlin-programming/interfaces) #### 在本文中,您将借助示例学习有关接口以及如何在 Kotlin 中实现接口的知识。 Kotlin 接口类似于 Java 8 中的接口。它们可以包含抽象方法的定义以及非抽象方法的实现。 但是,它们不能包含任何状态。 意思是,接口可能具有属性,但是它必须是抽象的,或者必须提供访问器实现。 * * * **推荐阅读**: [Kotlin 抽象类](/kotlin-programming/abstract-class "Kotlin Abstract Class") Kotlin 中的抽象类与接口相似,但有一个重要区别。 抽象类的属性不是抽象的或提供访问器实现都是强制性的。 * * * ## 如何定义接口? 关键字`interface`用于在 Kotlin 中定义接口。 例如, ```kt interface MyInterface { var test: String // abstract property fun foo() // abstract method fun hello() = "Hello there" // method with default implementation } ``` 这里, * 接口`MyInterface`已创建。 * 接口具有抽象属性`test`和抽象方法`foo()`。 * 该接口还具有非抽象方法`hello()`。 * * * ## 如何实现接口? 这是类或对象如何实现接口的方法: ```kt interface MyInterface { val test: Int // abstract property fun foo() : String // abstract method (returns String) fun hello() { // method with default implementation // body (optional) } } class InterfaceImp : MyInterface { override val test: Int = 25 override fun foo() = "Lol" // other code } ``` 在这里,类`InterfaceImp`实现了`MyInterface`接口。 该类覆盖接口的抽象成员(`test`属性和`foo()`方法)。 * * * ## 示例:接口如何工作? ```kt interface MyInterface { val test: Int fun foo() : String fun hello() { println("Hello there, pal!") } } class InterfaceImp : MyInterface { override val test: Int = 25 override fun foo() = "Lol" } fun main(args: Array) { val obj = InterfaceImp() println("test = ${obj.test}") print("Calling hello(): ") obj.hello() print("Calling and printing foo(): ") println(obj.foo()) } ``` 运行该程序时,输出为: ```kt test = 25 Calling hello(): Hello there, pal! Calling and printing foo(): Lol ``` * * * 如上所述,接口还可以具有提供访问器实现的属性。 例如, ```kt interface MyInterface { // property with implementation val prop: Int get() = 23 } class InterfaceImp : MyInterface { // class body } fun main(args: Array) { val obj = InterfaceImp() println(obj.prop) } ``` When you run the program, the output will be: ```kt 23 ``` 在这里,`prop`不是抽象的。 但是,它在接口内部有效,因为它为访问器提供了实现。 但是,您无法在界面内执行类似`val prop: Int = 23`的操作。 * * * ## 在一个类中实现两个或多个接口 Kotlin 不允许真正的[多重继承](https://en.wikipedia.org/wiki/Multiple_inheritance "Multiple Inheritance")。 但是,可以在一个类中实现两个或多个接口。 例如, ```kt interface A { fun callMe() { println("From interface A") } } interface B { fun callMeToo() { println("From interface B") } } // implements two interfaces A and B class Child: A, B fun main(args: Array) { val obj = Child() obj.callMe() obj.callMeToo() } ``` When you run the program, the output will be: ```kt From interface A From interface B ``` * * * ## 解决覆盖冲突(多个接口) 假设两个接口(`A`和`B`)具有同名的非抽象方法(假设`callMe()`方法)。 您在一个类中实现了这两个接口(假设`C`)。 现在,如果使用类`C`的对象调用`callMe()`方法,则编译器将引发错误。 例如, ```kt interface A { fun callMe() { println("From interface A") } } interface B { fun callMe() { println("From interface B") } } class Child: A, B fun main(args: Array) { val obj = Child() obj.callMe() } ``` 这是错误: ```kt Error:(14, 1) Kotlin: Class 'C' must override public open fun callMe(): Unit defined in A because it inherits multiple interface methods of it ``` * * * 要解决此问题,您需要提供自己的实现。 这是如何做: ```kt interface A { fun callMe() { println("From interface A") } } interface B { fun callMe() { println("From interface B") } } class C: A, B { override fun callMe() { super.callMe() super.callMe() } } fun main(args: Array) { val obj = C() obj.callMe() } ``` 现在,当您运行程序时,输出将是: ```kt From interface A From interface B ``` 在此,在类`C`中提供了`callMe()`方法的显式实现。 ```kt class C: A, B { override fun callMe() { super.callMe() super.callMe() } } ``` 语句`super.callMe()`调用类`A`的`callMe()`方法。 类似地,`super.callMe()`调用类`B`的`callMe()`方法。