解释一下这串代码的运行结果class A{ static { System.out.println(“我是A的静态代码块”); } A(){ System.out.println(“我是A的构造函数”); } } class B extends A{ static { System.out.println(“我是B的静态代码块”); } B(){ System.out.println(“我是B的构造函数”); } } class C extends B{ B b1=new B(); { System.out.println(“我是C的普通代码块”); } B b2=new B();
时间: 2023-04-02 14:02:57 浏览: 101
这段代码的运行结果是:
我是A的静态代码块
我是B的静态代码块
我是A的构造函数
我是B的构造函数
我是C的普通代码块
我是B的构造函数
首先,当程序运行时,会先执行 A 类的静态代码块,输出“我是A的静态代码块”。接着执行 B 类的静态代码块,输出“我是B的静态代码块”。
然后,创建 C 类的对象时,会先调用 B 类的构造函数,输出“我是B的构造函数”,再执行 C 类的普通代码块,输出“我是C的普通代码块”,最后再创建一个 B 类的对象,输出“我是B的构造函数”。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)