<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 平臺相關聲明 > 多平臺項目是 Kotlin 1.2 與 Kotlin 1.3 中的一個實驗性特性。本文檔中描述的所有語言與工具特性在未來的 Kotlin 版本中都可能會有所變化。 Kotlin 多平臺代碼的一個關鍵功能是讓公共代碼能夠依賴平臺相關聲明的一種方式。在其他語言中,這通常可以通過在公共代碼中構建一組接口并在平臺相關模塊中實現這些接口來完成。然而,當在其中某個平臺上有一個實現所需功能的庫,并且希望直接使用該庫的 API而無需額外包裝器時,這種方法并不理想。此外,它需要以接口表示公共聲明,這無法覆蓋所有可能情況。 作為替代方案,Kotlin 提供了一種 _預期聲明與實際聲明_ 的機制。利用這種機制,公共模塊可以定義 _預期聲明_ ,而平臺模塊可以提供與預期聲明相對應的 _實際聲明_ 。為了了解其工作機制,我們先來看一個示例。此代碼是公共模塊的一部分: ```kotlin package org.jetbrains.foo expect class Foo(bar: String) { fun frob() } fun main() { Foo("Hello").frob() } ``` 而這是相應的 JVM 模塊: ```kotlin package org.jetbrains.foo actual class Foo actual constructor(val bar: String) { actual fun frob() { println("Frobbing the $bar") } } ``` 這闡明了幾個要點: * 公共模塊中的預期聲明與其對應的實際聲明始終具有完全相同的完整限定名。 * 預期聲明標有 `expect` 關鍵字;實際聲明標有 `actual` 關鍵字。 * 與預期聲明的任何部分匹配的所有實際聲明都需要標記為 `actual`。 * 預期聲明決不包含任何實現代碼。 請注意,預期聲明并不限于接口與接口成員。在本例中,預期的類有一個構造函數,于是可以直接在公共代碼中創建該類。還可以將 `expect` 修飾符應用于其他聲明,包括頂層聲明以及注解: ```kotlin // 公共 expect fun formatString(source: String, vararg args: Any): String expect annotation class Test // JVM actual fun formatString(source: String, vararg args: Any) = String.format(source, *args) actual typealias Test = org.junit.Test ``` 編譯器會確保每個預期聲明在實現相應公共模塊的所有平臺模塊中都具有實際聲明,并且如果缺少任何實際聲明,編譯器都會報錯。IDE 提供了幫助創建所缺失實際聲明的工具。 如果有一個希望用在公共代碼中的平臺相關的庫,同時為其他平臺提供自己的實現,那么可以將現有類的別名作為實際聲明: ```kotlin expect class AtomicRef<V>(value: V) { fun get(): V fun set(value: V) fun getAndSet(value: V): V fun compareAndSet(expect: V, update: V): Boolean } actual typealias AtomicRef<V> = java.util.concurrent.atomic.AtomicReference<V> ```
                  <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>

                              哎呀哎呀视频在线观看