您的位置:首页 > 文旅 > 美景 > 淮北论坛创建文明城市_重庆怎样建设网站_seo学校培训课程_营销软文小短文

淮北论坛创建文明城市_重庆怎样建设网站_seo学校培训课程_营销软文小短文

2024/12/23 7:11:56 来源:https://blog.csdn.net/lft18/article/details/144119848  浏览:    关键词:淮北论坛创建文明城市_重庆怎样建设网站_seo学校培训课程_营销软文小短文
淮北论坛创建文明城市_重庆怎样建设网站_seo学校培训课程_营销软文小短文

Synthetic是人造,合成的意思,在虚拟机很多地方使用ACC_SYNTHETIC表示编译器自动生成的,区别于我们自己写的程序代码。这样说可能比较模糊,我们举个例子:我们创建一个内部类,如下

public class TestInnerClass {public void say(String msg) {System.out.println(msg);}class InnerClass {public void sayInner() {say("hello");}}
}

我们用javap看一下内部类InnerClass反编译之后的情况:

完整如下:

Classfile /F:/demo/mallsystem/provider/target/classes/com/mall/test/TestInnerClass$InnerClass.classLast modified 2024-11-28; size 638 bytesMD5 checksum 9f5efd7b8d0ad50545773d2a0e198f60Compiled from "TestInnerClass.java"
class com.mall.test.TestInnerClass$InnerClassminor version: 0major version: 52flags: ACC_SUPER
Constant pool:#1 = Fieldref           #5.#22         // com/mall/test/TestInnerClass$InnerClass.this$0:Lcom/mall/test/TestInnerClass;#2 = Methodref          #6.#23         // java/lang/Object."<init>":()V#3 = String             #24            // hello#4 = Methodref          #25.#26        // com/mall/test/TestInnerClass.say:(Ljava/lang/String;)V#5 = Class              #27            // com/mall/test/TestInnerClass$InnerClass#6 = Class              #28            // java/lang/Object#7 = Utf8               this$0#8 = Utf8               Lcom/mall/test/TestInnerClass;#9 = Utf8               <init>#10 = Utf8               (Lcom/mall/test/TestInnerClass;)V#11 = Utf8               Code#12 = Utf8               LineNumberTable#13 = Utf8               LocalVariableTable#14 = Utf8               this#15 = Utf8               InnerClass#16 = Utf8               InnerClasses#17 = Utf8               Lcom/mall/test/TestInnerClass$InnerClass;#18 = Utf8               sayInner#19 = Utf8               ()V#20 = Utf8               SourceFile#21 = Utf8               TestInnerClass.java#22 = NameAndType        #7:#8          // this$0:Lcom/mall/test/TestInnerClass;#23 = NameAndType        #9:#19         // "<init>":()V#24 = Utf8               hello#25 = Class              #29            // com/mall/test/TestInnerClass#26 = NameAndType        #30:#31        // say:(Ljava/lang/String;)V#27 = Utf8               com/mall/test/TestInnerClass$InnerClass#28 = Utf8               java/lang/Object#29 = Utf8               com/mall/test/TestInnerClass#30 = Utf8               say#31 = Utf8               (Ljava/lang/String;)V
{final com.mall.test.TestInnerClass this$0;descriptor: Lcom/mall/test/TestInnerClass;flags: ACC_FINAL, ACC_SYNTHETICcom.mall.test.TestInnerClass$InnerClass(com.mall.test.TestInnerClass);descriptor: (Lcom/mall/test/TestInnerClass;)Vflags:Code:stack=2, locals=2, args_size=20: aload_01: aload_12: putfield      #1                  // Field this$0:Lcom/mall/test/TestInnerClass;5: aload_06: invokespecial #2                  // Method java/lang/Object."<init>":()V9: returnLineNumberTable:line 12: 0LocalVariableTable:Start  Length  Slot  Name   Signature0      10     0  this   Lcom/mall/test/TestInnerClass$InnerClass;0      10     1 this$0   Lcom/mall/test/TestInnerClass;public void sayInner();descriptor: ()Vflags: ACC_PUBLICCode:stack=2, locals=1, args_size=10: aload_01: getfield      #1                  // Field this$0:Lcom/mall/test/TestInnerClass;4: ldc           #3                  // String hello6: invokevirtual #4                  // Method com/mall/test/TestInnerClass.say:(Ljava/lang/String;)V9: returnLineNumberTable:line 14: 0line 15: 9LocalVariableTable:Start  Length  Slot  Name   Signature0      10     0  this   Lcom/mall/test/TestInnerClass$InnerClass;
}
SourceFile: "TestInnerClass.java"
InnerClasses:#15= #5 of #25; //InnerClass=class com/mall/test/TestInnerClass$InnerClass of class com/mall/test/TestInnerClass

重点看下面标记的,ACC_SYNTHETIC标记出现了

这里编译器在内部类生成了一个TestInnerClass的引用对象this$0,因为是编译器自动合成的,所以标记了ACC_SYNTHETIC。

而下面是内部类的构造方法,可以看到它传入了外部类的实例对象,之后把它赋值给this$0

aload_0默认是实例方法第一个参数是this,即内部类自己的引用

aload_1则是加载第一个入参也就是外部类实例对象

putfield #1就是把外部实例设置给this$0

所以这也就解释了为什么内部类创建需要外部类对象创建了,如果外部类对象还没有生成,怎么传给内部类的构造方法呢?

而我们内部类能调用外部类的方法,也正是通过外部类的实例对象调用的

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com