<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 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<String>) { 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<String>) { val obj = InterfaceImp() println(obj.prop) } ``` 運行該程序時,輸出為: ```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<String>) { val obj = Child() obj.callMe() obj.callMeToo() } ``` 運行該程序時,輸出為: ```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<String>) { 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<A>.callMe() super<B>.callMe() } } fun main(args: Array<String>) { val obj = C() obj.callMe() } ``` 現在,當您運行程序時,輸出將是: ```kt From interface A From interface B ``` 在此,在類`C`中提供了`callMe()`方法的顯式實現。 ```kt class C: A, B { override fun callMe() { super<A>.callMe() super<B>.callMe() } } ``` 語句`super<A>.callMe()`調用類`A`的`callMe()`方法。 類似地,`super<B>.callMe()`調用類`B`的`callMe()`方法。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看