<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 功能強大 支持多語言、二開方便! 廣告
                ## 委托的定義 [TOC] 委托模式也叫代理模式,是最常用的設計模式的一種。在委托模式中,有兩個對象參與處理同一個請求,接受請求的對象將請求委托給另一個對象來處理。委托模式已證明是實現繼承的一個很好的替代方式。 委托在C#中是一個語言級特性。在Kotlin 也直接支持委托模式,更加優雅,簡潔。**Kotlin 通過關鍵字 by 實現委托**。 **Kotlin中委托分為類委托和委托屬性,Kotlin官方庫也封裝了一些常用的委托**。 ### 委托類 大家知道委托是由兩個對象完成的,因此可以推測出類委托實際上也包含兩個對象,一個是委托類,一個是被委托類。在委托類中并沒有真正的功能方法,該類的功能是通過調用被委托類中的方法實現的。接下來我們通過一個案例來學習類委托的實現: 假如有如下場景,“大頭兒子和小頭爸爸都會洗碗,圍裙媽媽讓小頭爸爸洗碗的時候,小頭爸爸就讓大頭兒子洗碗,然后會在大頭兒子洗碗之前,叮囑大頭兒子洗碗小心一點,在大頭兒子洗碗之后,檢查大頭兒子洗碗洗干凈沒有”。 #### Java語言的代理模式實現 如果用Java語言的代碼模式去實現,可以這樣做,參考代碼: ![](http://upload-images.jianshu.io/upload_images/7368752-40077e17b3738b2f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![](http://upload-images.jianshu.io/upload_images/7368752-430de42ba1ae0e32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) #### Kotlin語言的委托模式實現 第一種方式 ``` interface Wash { fun washDishes() } class Child : Wash { override fun washDishes() { println("委托小頭兒子洗碗,耶!") } } //第1種委托方式 class SmallFather : Wash by Child() {} fun main(args: Array<String>) { var smallFather = SmallFather() smallFather.washDishes() } ``` 運行結果 ``` 委托小頭兒子洗碗,耶! ``` 上述代碼中,定義了一個接口Wash,以及兩個接口的實現類Child和SmallFather,而SmallFather類中并沒有核心代碼,SmallFather類要實現的功能委托給Child類進行處理。在main()函數中,通過創建委托對象SmallFather,調用被委托類Child中的方法washDishes(),即可實現委托功能,輸出運行結果。 實際上類委托還有一種寫法,就是在委托類繼承接口的同時,傳入一個被委托類的實例對象。 如果用Kotlin語言的代碼模式去實現,可以這樣做,參考代碼: ![](http://upload-images.jianshu.io/upload_images/7368752-a02cc66c6e8819dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![](http://upload-images.jianshu.io/upload_images/7368752-3303c0f2eecf0a63.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 對比以上代碼以上代碼,我們可以看出【Kotlin相比于Java,實現委托代理模式,更加簡潔、優雅】 ?
                  <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>

                              哎呀哎呀视频在线观看