类的<cinit>()V方法、<init>()V、与方法调用

目录

()V方法

()V

 方法调用


<cinit>()V方法

一个类在加载的时候,编译器会按从上至下的顺序,收集所有 static 静态代码块静态成员赋值的代码,合并为一个特殊的方法 <cinit>()V :并调用。

 对应字节码

<init>()V

编译器会按从上至下的顺序,收集所有 {} 代码块和成员变量赋值的代码,形成新的构造方法,但原始构 造方法内的代码总是在最后
    private String a = "s1";

    {
        b = 20;
    }

    private int b = 10;

    {
        a = "s2";
    }

    {
    }

    public Demo3_8_2(String a, int b) {
        this.a = a;
        this.b = b;
    }

    public static void main(String[] args) {
        Demo3_8_2 d = new Demo3_8_2("s3", 30);
        System.out.println(d.a);
        System.out.println(d.b);
    }

对应字节码

 方法调用

对应字节码

  • new 是创建【对象】,给对象分配堆内存,执行成功会将【对象引用】压入操作数栈
  • dup 是复制操作数栈栈顶的内容,本例即为【对象引用】,为什么需要两份引用呢,一个是要配 invokespecial 调用该对象的构造方法 "<init>":()V (会消耗掉栈顶一个引用),另一个要 配合 astore_1 赋值给局部变量
  • 最终方法(fifinal),私有方法(private),构造方法都是由 invokespecial 指令来调用,属于静 。态绑定 普通成员方法是由 invokevirtual 调用,属于动态绑定,即支持多态
  • 成员方法与静态方法调用的另一个区别是,执行方法前是否需要【对象引用】 比较有意思的是 d.test4(); 是通过【对象引用】调用一个静态方法,可以看到在调用 invokestatic 之前执行了 pop 指令,把【对象引用】从操作数栈弹掉了,所以通过对象引用调用一个静态方法会产生两条无用指令。
  • 还有一个执行 invokespecial 的情况是通过 super 调用父类方法

-------------------------------------------------------------------------- InvalidDataError Traceback (most recent call last) File /usr/local/lib/python3.10/site-packages/imageio/plugins/pyav.py:308, in PyAVPlugin.__init__(self, request, container, **kwargs) 307 else: --> 308 self._container = av.open(request.get_file(), **kwargs) 309 self._video_stream = self._container.streams.video[0] File av/container/core.pyx:374, in av.container.core.open() File av/container/core.pyx:239, in av.container.core.Container.__cinit__() File av/container/core.pyx:259, in av.container.core.Container.err_check() File av/error.pyx:428, in av.error.err_check() InvalidDataError: [Errno 1094995529] Invalid data found when processing input: '<none>' During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) File /usr/local/lib/python3.10/site-packages/imageio/core/imopen.py:138, in imopen(uri, io_mode, plugin, extension, format_hint, legacy_mode, **kwargs) 137 try: --> 138 return loader(request, **kwargs) 139 except InitializationError as class_specific: File /usr/local/lib/python3.10/site-packages/imageio/core/imopen.py:130, in imopen.<locals>.loader(request, **kwargs) 129 def loader(request, **kwargs): --> 130 return config.plugin_class(request, **kwargs) File /usr/local/lib/python3.10/site-packages/imageio/plugins/pyav.py:311, in PyAVPlugin.__init__(self, request, container, **kwargs) 310 self._decoder = self._container.decode(video=0) --> 311 except av.AVError: 312 if isinstance(request.raw_uri, bytes): AttributeError: module 'av' has no attribute 'AVError' The above exception was the direct cause of the following exception: OSError Traceback (most recent call last) Cell In[8], line 39 32 user_msg = BaseMessage.make_user_message( 33 role_name="User", 34 content=
最新发布
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值