kotlin 构造函数、init、companion object用谁?

抛出问题:

  1. kotlin 构造函数干啥用的
  2. init和构造函数有啥区别
  3. 生命成员变量private var testString ?= null 啥时候分配空间
  4. companion object 加载时序

Q:kotlin 构造函数干啥用的
A:kotlin 的空参构造就是init方法, 结论:空参构造就是init方法

class testClass() {
    private var zdf:String?=null
    private var zdf01:String?=null
    init {
        zdf01 = "123"
        copy()
    }
    private fun copy(){
        zdf ="hei hei"
    }
}
---------------------------------------
public final class testClass {
   private String zdf;
   private String zdf01 = "123";
   private final void copy() {
      this.zdf = "hei hei";
   }
   public testClass() {
      this.copy();
   }
}

Q:kotlin 构造函数干啥用的
A: init和构造函数有啥区别。 结论:如果使用了多个构造方法,init为空参构造,剩下的构造方法该怎么实现怎么实现

class testClass() {
    private var zdf:String?=null
    private var zdf01:String?=null
    constructor(s: String,int: Int) : this() {

    }
    constructor(s:String) : this() {}

    init {
        zdf01 = "123"
        copy()
    }
    private fun copy(){
        zdf ="hei hei"
    }
}
-------------------------------
public final class testClass {
   private String zdf;
   private String zdf01;

   private final void copy() {
      this.zdf = "hei hei";
   }

   public testClass() {
      this.zdf01 = "123";
      this.copy();
   }

   public testClass(@NotNull String s, int var2) {
      Intrinsics.checkParameterIsNotNull(s, "s");
      this();
   }

   public testClass(@NotNull String s) {
      Intrinsics.checkParameterIsNotNull(s, "s");
      this();
   }
}

Q:声明成员变量private var testString ?= null 啥时候分配空间
A:成员变量在new的时候,使用new关键字构造对象时分配。优先于构造块和构造方法执行之前。

Q:companion object 加载时序
A: companion object 伴生对象,类似于静态方法
结论 : 修饰为伴生对象,伴生对象在类中只能存在一个,类似于java中的静态方法 Java 中使用类访问静态成员,静态方法。

class MainActivity : AppCompatActivity() {
    private var testString : String?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    init {
        testString = "zdf"
        println(testString)
        println(testInt)
    }
    companion object{
        const val testInt:Int = 1
    }
}
--------------------------------
public final class MainActivity extends AppCompatActivity {
   private String testString = "zdf";
   public static final int testInt = 1;
   public static final MainActivity.Companion Companion = new MainActivity.Companion((DefaultConstructorMarker)null);
   private HashMap _$_findViewCache;

   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.setContentView(-1300009);
   }

   public MainActivity() {
      String var1 = this.testString;
      boolean var2 = false;
      System.out.println(var1);
      byte var3 = 1;
      var2 = false;
      System.out.println(var3);
   }

   public View _$_findCachedViewById(int var1) {
      if (this._$_findViewCache == null) {
         this._$_findViewCache = new HashMap();
      }

      View var2 = (View)this._$_findViewCache.get(var1);
      if (var2 == null) {
         var2 = this.findViewById(var1);
         this._$_findViewCache.put(var1, var2);
      }

      return var2;
   }

   public void _$_clearFindViewByIdCache() {
      if (this._$_findViewCache != null) {
         this._$_findViewCache.clear();
      }

   }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。