您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Kotlin構造函數、成員變量和init代碼塊執行順序實例分析”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Kotlin構造函數、成員變量和init代碼塊執行順序實例分析”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
從Java語法的角度分析了Kotlin構造函數、成員變量初始化、init代碼塊三者的執行順序:
Kotlin構造函數與成員變量和init代碼塊執行順序詳細講解
這次再從字節碼的角度分析它們的執行順序。
還是用之前那個例子:
class InitOrderDemo(name: String) { val firstProperty = "First property: $name".also(::println) init { println("First initializer block that prints ${name}") } val secondProperty = "Second property: ${name.length}".also(::println) init { println("Second initializer block that prints ${name.length}") } }
調用InitOrderDemo(“hello”)
打印的結果如下:
First property: hello
First initializer block that prints hello
Second property: 5
Second initializer block that prints 5
可以看到執行順序,是按照它們聲明的順序執行。
將上面Koltin代碼轉成字節碼之后,顯示內容如下:
// ================com/devnn/javalib/InitOrderDemo.class ================= // class version 52.0 (52) // access flags 0x31 public final class com/devnn/javalib/InitOrderDemo { // access flags 0x12 private final Ljava/lang/String; firstProperty @Lorg/jetbrains/annotations/NotNull;() // invisible // access flags 0x11 public final getFirstProperty()Ljava/lang/String; @Lorg/jetbrains/annotations/NotNull;() // invisible L0 LINENUMBER 4 L0 ALOAD 0 GETFIELD com/devnn/javalib/InitOrderDemo.firstProperty : Ljava/lang/String; ARETURN L1 LOCALVARIABLE this Lcom/devnn/javalib/InitOrderDemo; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 // access flags 0x12 private final Ljava/lang/String; secondProperty @Lorg/jetbrains/annotations/NotNull;() // invisible // access flags 0x11 public final getSecondProperty()Ljava/lang/String; @Lorg/jetbrains/annotations/NotNull;() // invisible L0 LINENUMBER 10 L0 ALOAD 0 GETFIELD com/devnn/javalib/InitOrderDemo.secondProperty : Ljava/lang/String; ARETURN L1 LOCALVARIABLE this Lcom/devnn/javalib/InitOrderDemo; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 // access flags 0x1 public <init>(Ljava/lang/String;)V // annotable parameter count: 1 (visible) // annotable parameter count: 1 (invisible) @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0 L0 ALOAD 1 LDC "name" INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkNotNullParameter (Ljava/lang/Object;Ljava/lang/String;)V L1 LINENUMBER 3 L1 ALOAD 0 INVOKESPECIAL java/lang/Object.<init> ()V L2 LINENUMBER 4 L2 ALOAD 0 NEW java/lang/StringBuilder DUP INVOKESPECIAL java/lang/StringBuilder.<init> ()V LDC "First property: " INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; ALOAD 1 INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String; ASTORE 2 L3 ALOAD 2 ASTORE 3 L4 LINENUMBER 17 L4 ASTORE 5 L5 ICONST_0 ISTORE 4 L6 LINENUMBER 4 L6 L7 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; ALOAD 3 INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/Object;)V L8 L9 L10 GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit; ASTORE 6 ALOAD 5 L11 LINENUMBER 4 L11 L12 ALOAD 2 L13 PUTFIELD com/devnn/javalib/InitOrderDemo.firstProperty : Ljava/lang/String; L14 LINENUMBER 6 L14 NOP L15 LINENUMBER 7 L15 NEW java/lang/StringBuilder DUP INVOKESPECIAL java/lang/StringBuilder.<init> ()V LDC "First initializer block that prints " INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; ALOAD 1 INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String; ASTORE 2 L16 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; ALOAD 2 INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/Object;)V L17 L18 LINENUMBER 8 L18 NOP L19 LINENUMBER 10 L19 ALOAD 0 NEW java/lang/StringBuilder DUP INVOKESPECIAL java/lang/StringBuilder.<init> ()V LDC "Second property: " INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; ALOAD 1 INVOKEVIRTUAL java/lang/String.length ()I INVOKEVIRTUAL java/lang/StringBuilder.append (I)Ljava/lang/StringBuilder; INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String; ASTORE 2 L20 ALOAD 2 ASTORE 3 L21 LINENUMBER 17 L21 ASTORE 5 L22 ICONST_0 ISTORE 4 L23 LINENUMBER 10 L23 L24 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; ALOAD 3 INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/Object;)V L25 L26 L27 GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit; ASTORE 6 ALOAD 5 L28 LINENUMBER 10 L28 L29 ALOAD 2 L30 PUTFIELD com/devnn/javalib/InitOrderDemo.secondProperty : Ljava/lang/String; L31 LINENUMBER 12 L31 NOP L32 LINENUMBER 13 L32 NEW java/lang/StringBuilder DUP INVOKESPECIAL java/lang/StringBuilder.<init> ()V LDC "Second initializer block that prints " INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; ALOAD 1 INVOKEVIRTUAL java/lang/String.length ()I INVOKEVIRTUAL java/lang/StringBuilder.append (I)Ljava/lang/StringBuilder; INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String; ASTORE 2 L33 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; ALOAD 2 INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/Object;)V L34 L35 LINENUMBER 14 L35 RETURN L36 LOCALVARIABLE p1 Ljava/lang/Object; L5 L10 3 LOCALVARIABLE $i$a$-unknown-InitOrderDemo$firstProperty$1 I L6 L10 4 LOCALVARIABLE p1 Ljava/lang/Object; L22 L27 3 LOCALVARIABLE $i$a$-unknown-InitOrderDemo$secondProperty$1 I L23 L27 4 LOCALVARIABLE this Lcom/devnn/javalib/InitOrderDemo; L0 L36 0 LOCALVARIABLE name Ljava/lang/String; L0 L36 1 MAXSTACK = 3 MAXLOCALS = 7 }
可以看到上面的構造函數、成員變量初始化和init代碼塊,按照聲明都被放到了字節碼的init代碼塊中了。
字節碼的init初始化器其實就是類的構造函數。將Java代碼轉成字節碼也是存在init構造函數。
下面看一個Java示例,加深對字節碼的init初始化塊的認識。
package com.devnn.javalib; public class JavaInit { String firstName = "Steven"; { System.out.println("This is init block"); } JavaInit(String secondName) { System.out.println("firstName=" + firstName); System.out.println("secondName=" + secondName); } public static void main(String[] args) { new JavaInit("Jobs"); } }
運行main函數打印結果如下:
This is init block
firstName=Steven
secondName=Jobs
將上面的JavaInit
類轉成字節碼之后的內容如下:
// class version 51.0 (51) // access flags 0x21 public class com/devnn/javalib/JavaInit { // compiled from: JavaInit.java // access flags 0x0 Ljava/lang/String; firstName // access flags 0x0 <init>(Ljava/lang/String;)V L0 LINENUMBER 10 L0 ALOAD 0 INVOKESPECIAL java/lang/Object.<init> ()V L1 LINENUMBER 4 L1 ALOAD 0 LDC "Steven" PUTFIELD com/devnn/javalib/JavaInit.firstName : Ljava/lang/String; L2 LINENUMBER 7 L2 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; LDC "This is init block" INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V L3 LINENUMBER 11 L3 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; NEW java/lang/StringBuilder DUP INVOKESPECIAL java/lang/StringBuilder.<init> ()V LDC "firstName=" INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; ALOAD 0 GETFIELD com/devnn/javalib/JavaInit.firstName : Ljava/lang/String; INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String; INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V L4 LINENUMBER 12 L4 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; NEW java/lang/StringBuilder DUP INVOKESPECIAL java/lang/StringBuilder.<init> ()V LDC "secondName=" INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; ALOAD 1 INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String; INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V L5 LINENUMBER 13 L5 RETURN L6 LOCALVARIABLE this Lcom/devnn/javalib/JavaInit; L0 L6 0 LOCALVARIABLE secondName Ljava/lang/String; L0 L6 1 MAXSTACK = 3 MAXLOCALS = 2 // access flags 0x9 public static main([Ljava/lang/String;)V L0 LINENUMBER 16 L0 NEW com/devnn/javalib/JavaInit DUP LDC "Jobs" INVOKESPECIAL com/devnn/javalib/JavaInit.<init> (Ljava/lang/String;)V POP L1 LINENUMBER 17 L1 RETURN L2 LOCALVARIABLE args [Ljava/lang/String; L0 L2 0 MAXSTACK = 3 MAXLOCALS = 1 }
可見,Java類的成員變量初始化、構造函數、構造塊同樣都被拷貝進了init代碼塊中。那么它們是否存在順序問題呢?
將上面JavaInit類的firname成員變量放到初始化塊下面試試:
package com.devnn.javalib; public class JavaInit { { System.out.println("This is init block"); } JavaInit(String secondName) { System.out.println("firstName=" + firstName); System.out.println("secondName=" + secondName); } String firstName = "Steven"; public static void main(String[] args) { new JavaInit("Jobs"); } }
查看字節碼:
// class version 51.0 (51) // access flags 0x21 public class com/devnn/javalib/JavaInit { // compiled from: JavaInit.java // access flags 0x0 Ljava/lang/String; firstName // access flags 0x0 <init>(Ljava/lang/String;)V L0 LINENUMBER 8 L0 ALOAD 0 INVOKESPECIAL java/lang/Object.<init> ()V L1 LINENUMBER 5 L1 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; LDC "This is init block" INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V L2 LINENUMBER 13 L2 ALOAD 0 LDC "Steven" PUTFIELD com/devnn/javalib/JavaInit.firstName : Ljava/lang/String; L3 LINENUMBER 9 L3 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; NEW java/lang/StringBuilder DUP INVOKESPECIAL java/lang/StringBuilder.<init> ()V LDC "firstName=" INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; ALOAD 0 GETFIELD com/devnn/javalib/JavaInit.firstName : Ljava/lang/String; INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String; INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V L4 LINENUMBER 10 L4 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; NEW java/lang/StringBuilder DUP INVOKESPECIAL java/lang/StringBuilder.<init> ()V LDC "secondName=" INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; ALOAD 1 INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder; INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String; INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V L5 LINENUMBER 11 L5 RETURN L6 LOCALVARIABLE this Lcom/devnn/javalib/JavaInit; L0 L6 0 LOCALVARIABLE secondName Ljava/lang/String; L0 L6 1 MAXSTACK = 3 MAXLOCALS = 2 // access flags 0x9 public static main([Ljava/lang/String;)V L0 LINENUMBER 17 L0 NEW com/devnn/javalib/JavaInit DUP LDC "Jobs" INVOKESPECIAL com/devnn/javalib/JavaInit.<init> (Ljava/lang/String;)V POP L1 LINENUMBER 18 L1 RETURN L2 LOCALVARIABLE args [Ljava/lang/String; L0 L2 0 MAXSTACK = 3 MAXLOCALS = 1 }
可見,Java類的成員變量初始化、構造函數、構造塊同樣都被拷貝進了字節碼init代碼塊中。Java的成員變量初始化和構造塊也是按聲明順序執行。不同的是,Java的構造函數代碼始終放在了字節碼init代碼塊的后面。
字節碼的init初始化塊,其實就是類的真正的構造函數。Kotlin多個init代碼塊都是按照順序拷貝進了字節碼的init初始化塊中,可以理解為它們是構造函數的組成部分。
Java和kotlin成員變量的初始化都是放到字節碼的init代碼塊中,也就是在構造函數中執行的。
讀到這里,這篇“Kotlin構造函數、成員變量和init代碼塊執行順序實例分析”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。