Kotlin Extension — Declaring Extensions as Members

2019-10-10 17:36栏目:www.qy66.vip
TAG:

鉴于官方网站的例子很周到,由此这里直接套用Kotlin官方网站的例子:

open class D {
}

class D1 : D() {
}

open class C {
    open fun D.foo() {
        println("D.foo in C")
    }

    open fun D1.foo() {
        println("D1.foo in C")
    }

    fun caller(d: D) {
        d.foo()   // call the extension function
    }
}

class C1 : C() {
    override fun D.foo() {
        println("D.foo in C1")
    }

    override fun D1.foo() {
        println("D1.foo in C1")
    }
}

C().caller(D())   // prints "D.foo in C"
C1().caller(D())  // prints "D.foo in C1" - dispatch receiver is resolved virtually
C().caller(D1())  // prints "D.foo in C" - extension receiver is resolved statically

事例很简短,无非正是Extension的注脚和如何调用Extension方法(注意:这里调用Members' Extension 通过了实例方法的中间转播,可以跟Kotlin Extension - Method中涉嫌的江淹梦笔在.kt文件中从实例可能外部平素获得Extension方法援引联系起来)
这里Kotlin定义了五个根本的定义:

dispatch receiver: 指的就是例证中进行caller() 方法的C或许C1实例对象
extension receiver: 指的就是例证中caller() 方法定义中实行Extension方法foo() 的对象

接下去看看多少个例证给大家揭破的局地非同儿戏气象

1. C().caller(D1())

很鲜明,我们这里往caller()方法里面传的是extension receiver类型是D1,可是怎么最后调用的是父类D的Extension方法呢?
由此反编写翻译生成的.class文件大家找到的因由所在

//C.class
...
public final void caller(com.maxtropy.viewtest.D);
    Code:
       0: aload_1
       1: ldc           #39                 // String d
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_1
       8: invokevirtual #41                 // Method foo:(Lcom/maxtropy/viewtest/D;)V
      11: return
...

//.class where C().caller(D1()) was executed
...
      32: invokespecial #20                 // Method com/maxtropy/viewtest/C."<init>":()V
      35: new           #29                 // class com/maxtropy/viewtest/D1
      38: dup
      39: invokespecial #30                 // Method com/maxtropy/viewtest/D1."<init>":()V
      42: checkcast     #22                 // class com/maxtropy/viewtest/D
      45: invokevirtual #27                 // Method com/maxtropy/viewtest/C.caller:(Lcom/maxtropy/viewtest/D;)V
...

在caller()的入参中,d的参数类型是D,由此在调用caller()时,入参实例的项目就早就被上转型成D了。

2. C1().caller(D())

程序试行的结果很显明的告诉大家这是动态选用的结果,实施的是C第11中学的Extension方法,并不是父类C中的Extension方法。
哇!在 top-level Extension反编写翻译的例证中很扎眼的看见Extension方法被编写翻译成了静态方法,这里怎么又跟动态选取有关了吗???
同样施以反编写翻译的法力:

//C.class
Compiled from "C.kt"
public class com.maxtropy.viewtest.C {
  public void foo(com.maxtropy.viewtest.D);
    Code:
       0: aload_1
       1: ldc           #9                  // String $receiver
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #17                 // String D.foo in C
       8: astore_2
       9: getstatic     #23                 // Field java/lang/System.out:Ljava/io/PrintStream;
      12: aload_2
      13: invokevirtual #29                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      16: return

  public void foo(com.maxtropy.viewtest.D1);
    Code:
       0: aload_1
       1: ldc           #9                  // String $receiver
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #35                 // String D1.foo in C
       8: astore_2
       9: getstatic     #23                 // Field java/lang/System.out:Ljava/io/PrintStream;
      12: aload_2
      13: invokevirtual #29                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      16: return

  public final void caller(com.maxtropy.viewtest.D);
    Code:
       0: aload_1
       1: ldc           #39                 // String d
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_1
       8: invokevirtual #41                 // Method foo:(Lcom/maxtropy/viewtest/D;)V
      11: return

  public com.maxtropy.viewtest.C();
    Code:
       0: aload_0
       1: invokespecial #45                 // Method java/lang/Object."<init>":()V
       4: return
}

//C1.class
Compiled from "C.kt"
public final class com.maxtropy.viewtest.C1 extends com.maxtropy.viewtest.C {
  public void foo(com.maxtropy.viewtest.D);
    Code:
       0: aload_1
       1: ldc           #9                  // String $receiver
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #17                 // String D.foo in C1
       8: astore_2
       9: getstatic     #23                 // Field java/lang/System.out:Ljava/io/PrintStream;
      12: aload_2
      13: invokevirtual #29                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      16: return

  public void foo(com.maxtropy.viewtest.D1);
    Code:
       0: aload_1
       1: ldc           #9                  // String $receiver
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #35                 // String D1.foo in C1
       8: astore_2
       9: getstatic     #23                 // Field java/lang/System.out:Ljava/io/PrintStream;
      12: aload_2
      13: invokevirtual #29                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      16: return

  public com.maxtropy.viewtest.C1();
    Code:
       0: aload_0
       1: invokespecial #40                 // Method com/maxtropy/viewtest/C."<init>":()V
       4: return
}

原本评释在Member中的Extension方法漫天被编写翻译成了Member所在品种的实例方法!
那实例方法有啥用啊?(再一次重复这一个实例方法在写.kt时不可能通超过实际例直接得到援引,也就c.foo()是不可能经过编写翻译的)
神秘便在call()方法的第6、行, 第6行的aload_0指的就是this, 也等于caller()被试行时确实的实例对象dispatch receiver。就是this,让Extension方法具备了动态选取的技艺。

版权声明:本文由千亿游戏官网发布于www.qy66.vip,转载请注明出处:Kotlin Extension — Declaring Extensions as Members