An overview of the

Kotlin can extend the attributes and methods of a class without affecting the existing class

Extension methods

Extension methods can add new methods to an existing class without affecting the existing class

It is written as follows:

fun receiverType.functionName(params){
    body
}
Copy the code
  • ReceiverType: indicates the function receiver, that is, the name of the class to which the method is to be extended
  • FunctionName: Indicates the name of an extension function
  • Params: indicates the parameter of the extension function, which can be null

Here’s an example:

class A(var name111: String) {

}

fun A.printMsg(a) {
    println("Print$name111")}fun main(a) {
    A("Xiao Ming").printMsg()

}
Copy the code

You can see that class A doesn’t have any methods, and then you can use Kotlin’s feature to extend the printMsg method to A, and then you can call the extension method from A’s object

Take a look at decompiling Java code

public final class AKt {
   public static final void printMsg(@NotNull A $this$printMsg) {
      Intrinsics.checkNotNullParameter($this$printMsg, "$this$printMsg");
      String var1 = "Print" + $this$printMsg.getName111();
      boolean var2 = false;
      System.out.println(var1);
   }

   public static final void main(a) {
      printMsg(new A("Xiao Ming"));
   }

   // $FF: synthetic method
   public static void main(String[] var0) { main(); }}// A.java


public final class A {
   @NotNull
   private String name111;

   @NotNull
   public final String getName111(a) {
      return this.name111;
   }

   public final void setName111(@NotNull String var1) {
      Intrinsics.checkNotNullParameter(var1, "
      
       "
      ?>);
      this.name111 = var1;
   }

   public A(@NotNull String name111) {
      Intrinsics.checkNotNullParameter(name111, "name111");
      super(a);this.name111 = name111; }}Copy the code

You can see it’s just adding A static method and passing in the object of A

Extension functions are not polymorphic

Go straight to code

open class C

class D : C(a)fun C.text(a){
    println("I'm a c")}fun D.text(a){
    println("I am d")}fun haha(c:C){
    c.text()
}

fun main(a){
    haha(C())
    haha(D())
}
Copy the code

The output

I'm C. I'm CCopy the code

If the extension function has the same name as the member function, the member function is preferred

class E {
    fun text(a){
        println("Member function")}}fun E.text(a){
    println("Extension method")}fun main(a){
    E().text()
}
Copy the code

The output

A member functionCopy the code

Extending empty objects

class F {
    companion object {
        fun get(a): F? {
            return null}}}funF? .text(a): String {
    if (this= =null) {
        return "Empty"
    }

    return "Not empty"
}

fun main(a) {

    var f = F.get()

    println(f.text())

}
Copy the code

print

emptyCopy the code

Extended attributes


class E

var E.name: Int
    get(a)= 1
    set(value) {
        println(value)
    }



fun main(a) {
    E().name=111
    println(E().name)
}
Copy the code

Note that extended properties have no background fields, so you need to implement the GET and set methods

Decompile Java code

public final class EKt {
   public static final int getName(@NotNull E $this$name) {
      Intrinsics.checkNotNullParameter($this$name, "$this$name");
      return 1;
   }

   public static final void setName(@NotNull E $this$name, int value) {
      Intrinsics.checkNotNullParameter($this$name, "$this$name");
      boolean var2 = false;
      System.out.println(value);
   }

   public static final void main(a) {
      setName(new E(), 111);
      int var0 = getName(new E());
      boolean var1 = false;
      System.out.println(var0);
   }

   // $FF: synthetic method
   public static void main(String[] var0) { main(); }}// E.java



public final class E {}Copy the code

We’re actually adding static methods on top of set and get, and passing in E