美文网首页kotlin
kotlin-init、constructor、companio

kotlin-init、constructor、companio

作者: 有腹肌的豌豆Z | 来源:发表于2021-01-11 09:09 被阅读0次
class Apple {
 
    private var speak: String = "小啊小苹果"
 
    /* 主构造方法*/
    constructor(){
        println("主构造方法constructor()调用")
    }
 
    /* 次构造方法*/
    constructor(name:String, age:Int):this(){
        println("次带参数构造方法constructor(name:$name, age:$age)")
    }
 
    init {
        println("Apple init")
    }
 
    init {
        println("Apple init:name:${speak}")
    }
 
    /*伴生对象*/
    companion object {
        val instance: Apple by lazy {
            Apple("wqq", 100)
        }
 
        /*伴生对象中的初始化代码*/
        init {
            println("companion init 1")
        }
 
        init {
            println("companion init 2")
        }
    }
}

调用App() ,出现结果:

exclude patterns:
companion init 1
companion init 2
Apple init
Apple init:name:小啊小苹果
主构造方法constructor()调用

调用有参构造方法,出现结果

exclude patterns:
companion init 1
companion init 2
Apple init
Apple init:name:小啊小苹果
主构造方法constructor()调用
次带参数构造方法constructor(name:heihei, age:28)

可以看看 kotlin转java的代码:

import kotlin.Lazy;
import kotlin.LazyKt;
import kotlin.Metadata;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
 
@Metadata(
   mv = {1, 1, 18},
   bv = {1, 0, 3},
   k = 1,
   d1 = {"\u0000\u001a\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0000\n\u0002\u0010\b\n\u0002\b\u0004\u0018\u0000 \t2\u00020\u0001:\u0001\tB\u0007\b\u0016¢\u0006\u0002\u0010\u0002B\u0017\b\u0016\u0012\u0006\u0010\u0003\u001a\u00020\u0004\u0012\u0006\u0010\u0005\u001a\u00020\u0006¢\u0006\u0002\u0010\u0007R\u000e\u0010\b\u001a\u00020\u0004X\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006\n"},
   d2 = {"Lcom/androidtv/pos/Apple;", "", "()V", "name", "", "age", "", "(Ljava/lang/String;I)V", "speak", "Companion", "app"}
)
public final class Apple {
   private String speak;
   @NotNull
   private static final Lazy instance$delegate;
   public static final Apple.Companion Companion = new Apple.Companion((DefaultConstructorMarker)null);
 
   public Apple() {
      this.speak = "小啊小苹果";
      String var1 = "Apple init";
      boolean var2 = false;
      System.out.println(var1);
      var1 = "Apple init:name:" + this.speak;
      var2 = false;
      System.out.println(var1);
      var1 = "主构造方法constructor()调用";
      var2 = false;
      System.out.println(var1);
   }
 
   public Apple(@NotNull String name, int age) {
      Intrinsics.checkParameterIsNotNull(name, "name");
      this();
      String var3 = "次带参数构造方法constructor(name:" + name + ", age:" + age + ')';
      boolean var4 = false;
      System.out.println(var3);
   }
 
   static {
      instance$delegate = LazyKt.lazy((Function0)null.INSTANCE);
      String var0 = "companion init 1";
      boolean var1 = false;
      System.out.println(var0);
      var0 = "companion init 2";
      var1 = false;
      System.out.println(var0);
   }
 
   @Metadata(
      mv = {1, 1, 18},
      bv = {1, 0, 3},
      k = 1,
      d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u001b\u0010\u0003\u001a\u00020\u00048FX\u0086\u0084\u0002¢\u0006\f\n\u0004\b\u0007\u0010\b\u001a\u0004\b\u0005\u0010\u0006¨\u0006\t"},
      d2 = {"Lcom/androidtv/pos/Apple$Companion;", "", "()V", "instance", "Lcom/androidtv/pos/Apple;", "getInstance", "()Lcom/androidtv/pos/Apple;", "instance$delegate", "Lkotlin/Lazy;", "app"}
   )
   public static final class Companion {
      @NotNull
      public final Apple getInstance() {
         Lazy var1 = Apple.instance$delegate;
         Apple.Companion var2 = Apple.Companion;
         Object var3 = null;
         boolean var4 = false;
         return (Apple)var1.getValue();
      }
 
      private Companion() {
      }
 
      // $FF: synthetic method
      public Companion(DefaultConstructorMarker $constructor_marker) {
         this();
      }
   }
}

总结:由于伴生对象中的代码是在类加载时就会执行,所以伴生类初始化会先执行,再执行本身构造方法, 会把init代码 合并到构造方法中,但会插到构造方法最前面。

相关文章

  • kotlin-init、constructor、companio

    调用App() ,出现结果: exclude patterns:companion init 1companion...

  • kotlin学习日记(3)

    伴生对象 ●伴生对象中的公共函数必须带有 @JvmStatic 注释才能作为静态方法公开。 ●在 companio...

  • Kotlin : object、companion object

    object关键字主要有三种使用场景 对象声明(object declaration) 伴生对象(companio...

  • constructor()

    constructor里的this.state和直接写this.state区别?答案:没有区别 在React中co...

  • constructor

    constructor 为什么x会有constructor属性。因为每一个构造函数原型都会生成constructo...

  • constructor

    constructor 翻译:构造者、构造器语法:object.constructor 输出:function e...

  • constructor

    所有的函数都有一个prototype属性,它是一个对象。 prototype有一个constructor的属性,默...

  • Constructor

    this:继承同一类中的其它构造函数 Class Car{private string _description;...

  • constructor()

    constructor()是类的默认方法,通过new命令生成对象实例时,自动调用该方法。一个类必须有constru...

  • Haskell类构造器和值构造器的区别

    IO is a type constructor, not a value constructor. Type c...

网友评论

    本文标题:kotlin-init、constructor、companio

    本文链接:https://www.haomeiwen.com/subject/tfllaktx.html