Kotlin interface default method

Kotlin interface default method

前言

java 在 1.8 之前,interface 是沒(méi)有默認(rèn)方法的。但是 kotlin 是支持在接口中定義一個(gè)默認(rèn)方法的。那么 kotlin 是怎么實(shí)現(xiàn)的呢?本文將帶你一探究竟。

Show me the code


interface TestInterface {
    fun method1() {
        println("default impl for method1() called")
    }

    fun method2()
}

class TestInterfaceCaller {
    fun getTestInterfaceImpl(): TestInterface {
        return object : TestInterface {
            override fun method2() {
                println("method2 called")
            }
        }
    }
}

如代碼所示,我們定義了一個(gè)簡(jiǎn)單的 interface 叫 TestInterface,然后在里面有兩個(gè)方法:method1 和 method2 。其中,method1 是默認(rèn)方法,提供了一個(gè)默認(rèn)實(shí)現(xiàn)。然后,在使用方,也就是 TestInterfaceCaller 中,返回了一個(gè) object : TestInterface對(duì)象。這個(gè)對(duì)象就相當(dāng)于 java 中的匿名內(nèi)部類(lèi)。然后,我們發(fā)現(xiàn),ide 要求我們必須要實(shí)現(xiàn) method2 方法,而 method1 方法不用實(shí)現(xiàn)。這符合我們的直觀(guān)認(rèn)知:因?yàn)?method1 方法是當(dāng)做一個(gè)接口的默認(rèn)方法來(lái)用的,method2 方法是一個(gè)接口的普通方法,所有實(shí)現(xiàn)這個(gè)接口的對(duì)象都要實(shí)現(xiàn)這個(gè) method2 方法。

kotlin 怎么實(shí)現(xiàn)的?源碼面前,了無(wú)秘密。

kotlin 是怎么實(shí)現(xiàn) interface 默認(rèn)方法的呢?要知道 java1.8 才實(shí)現(xiàn)的功能,kotlin 其實(shí)不要求版本就能實(shí)現(xiàn)。jvm 又不可能針對(duì) kotlin 來(lái)單獨(dú)開(kāi)一個(gè)后門(mén)。要想知道 kotlin 是怎么實(shí)現(xiàn)的,我們需要對(duì)相關(guān)代碼做一個(gè)反編譯。結(jié)果如下:

public interface TestInterface {
   void method1();

   void method2();

   public static final class DefaultImpls {
      public static void method1(TestInterface $this) {
         String var1 = "default impl for method1() called";
         boolean var2 = false;
         System.out.println(var1);
      }
   }
}
public final class TestInterfaceCaller {
   @NotNull
   public final TestInterface getTestInterfaceImpl() {
      return (TestInterface)(new TestInterface() {
         public void method2() {
            String var1 = "method2 called";
            boolean var2 = false;
            System.out.println(var1);
         }

         public void method1() {
            TestInterface.DefaultImpls.method1(this);
         }
      });
   }
}

源碼面前,了無(wú)秘密。我們可以看到,對(duì)于 interface 中的默認(rèn)方法 method1,kotlin 其實(shí)有一個(gè) trick:在編譯期間會(huì)生成一個(gè) DefaultImpls 的靜態(tài)類(lèi),在里面有 method1 的實(shí)現(xiàn)。而在使用方,會(huì)在編譯期間進(jìn)行替換,把未實(shí)現(xiàn)的方法實(shí)現(xiàn)為對(duì) DefaultImpls 中的靜態(tài)方法的調(diào)用。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 寫(xiě)在開(kāi)頭:本人打算開(kāi)始寫(xiě)一個(gè)Kotlin系列的教程,一是使自己記憶和理解的更加深刻,二是可以分享給同樣想學(xué)習(xí)Kot...
    胡奚冰閱讀 1,517評(píng)論 5 11
  • Kotlin的類(lèi)和接口與Java的類(lèi)和接口是有一定的區(qū)別的。Kotlin的接口是可以包含屬性聲明。Kotlin默認(rèn)...
    程自舟閱讀 10,520評(píng)論 0 11
  • 面向?qū)ο缶幊蹋∣OP) 在前面的章節(jié)中,我們學(xué)習(xí)了Kotlin的語(yǔ)言基礎(chǔ)知識(shí)、類(lèi)型系統(tǒng)、集合類(lèi)以及泛型相關(guān)的知識(shí)。...
    Tenderness4閱讀 4,607評(píng)論 1 6
  • 簡(jiǎn)述: 從這篇文章將繼續(xù)開(kāi)始探索Kotlin中的一些高級(jí)的內(nèi)容,之前有著重探討了Kotlin的泛型以及泛型型變等內(nèi)...
    熊喵先森閱讀 6,258評(píng)論 1 8
  • @高一慧@周君[擁抱][擁抱]好多人都還沒(méi)睡啊, 今天怎么 怎么 都睡不著 一次次 重復(fù)聽(tīng)著《九兒》 這首歌曲 自...
    福娃婧閱讀 213評(píng)論 0 0

友情鏈接更多精彩內(nèi)容