(Vii) static member objects associated with the basic syntax of Kotlin

Static implementation of Kotlin

In Java can be declared by the static keyword static property or method. But in Kotlin and we did not continue to use this keyword, but the use of the associated object implementation, in the class declared inside a companion object code block, members of its internal variables and methods will be compiled into static.

class TestStatic {
    //伴生对象
    companion object Factory {
        val str: String = ""
        fun create(): TestStatic {
            println(this)
            return TestStatic()
        }
    }
}

Factory is the ultimate generated static inner class class name, generally speaking the name can be omitted, if omitted, the default class named Companion.

Call methods, just as calling a static method name + class method name.

TestStatic.create()

And the difference between the internal object class

Decompile we can look at its internal implementation.

public final class TestStatic {
   //静态成员变量
   private static final String str = "";
   public static final TestStatic.Factory Factory = new TestStatic.Factory();

   public static final class Factory {
      @NotNull
      public final String getStr() {
         return TestStatic.str;
      }

      @NotNull
      public final TestStatic create() {
         System.out.println(this);
         return new TestStatic();
      }

      private Factory() {
      }
   }
}

And the feeling is not a bit like talking about the object class? In fact, it really is quite like, because he sent a keyword, we companion leave out the word, re-decompile compare.

class TestStatic {
    object Factory {
        val str: String = ""
        fun create(): TestStatic {
            println(this)
            return TestStatic()
        }
    }
}

Decompile results:

public final class TestStatic {
   
   public static final class Factory {
      //成员变量声明在内部类中
      private static final String str = "";
      public static final TestStatic.Factory INSTANCE;

      @NotNull
      public final String getStr() {
         return str;
      }

      @NotNull
      public final TestStatic create() {
         System.out.println(this);
         return new TestStatic();
      }

      private Factory() {
      }

      static {
         TestStatic.Factory var0 = new TestStatic.Factory();
         INSTANCE = var0;
         str = "";
      }
   }
}

By comparing the code found significant differences, although both will eventually generate a static inner classes, but the companion object member variables in the code block will eventually be declared outside the class, which is why we also can directly outside the class name access, and object classes you need to use an external class. call the inner classes.

//object类调用方式
TestStatic.Factory.create()
//companion object代码块使用方式
TestStatic.create();//编译器会我们自动补全为TestStatic.Factory.create()

Precautions

  1. A class can have at most a companion object code block .
  2. Companion on the object is essentially a static inner class, so it can inherit from other classes, the following wording is also possible.
companion object : Comparable<String> {
    override fun compareTo(other: String): Int {
        //支持比较
    }
}

Reproduced in: https: //www.jianshu.com/p/ac0c25091491

Guess you like

Origin blog.csdn.net/weixin_33953384/article/details/91142113