当前位置:网站首页>Subclass a inherits from parent class B, a a = new a(); Then the execution sequence of the constructor of parent class B, the static code block of parent class B, the non static code block of parent c
Subclass a inherits from parent class B, a a = new a(); Then the execution sequence of the constructor of parent class B, the static code block of parent class B, the non static code block of parent c
2022-06-25 11:20:00 【User 9854323】
(1) Subclass A Inherited parent class B, A a = new A(); be :
Parent class B Static code block -> Subclass A Static code block -> Parent class B Non static code block -> Parent class B Constructors -> Subclass A Non static code block -> Subclass A Constructors
(2) If a subclass constructor explicitly calls a constructor of the parent class , Then call the constructor
class C {
C() {
System.out.print("C");
}
}
class A {
C c = new C();
A() {
this("A");
System.out.print("A");
}
A(String s) {
System.out.print(s);
}
}
class Test extends A {
Test() {
super("B");
System.out.print("B");
}
public static void main(String[] args) {
new Test();
}
}CBB First new A subclass object , Then you need to call the constructor to initialize the subclass object , But this class inherits from A, So you need to call the constructor of the parent class first , Through here super(“B”) The shown calls the parameterized construct of the parent class . Initialize the objects in the parent class before executing the parameterized construction of the parent class , To the c Members are initialized , Called C Nonparametric construction of class , So the call order is : First call C Nonparametric construction of class Call again A Class with parameters Finally, call the construction of subclasses
(3) stay JVM call mian Method is used to initialize the static content . In sequence : Static variable of parent class , The static code block of the parent class , Static variables of subclasses , Static code block of subclass .
public class Test{
static int cnt = 6;
static{
cnt += 9;
}
public static void main(string[] args){
System.out.println(“cnt =” + cnt);
}
static{
Cnt /=3;
};
}Cnt=5
边栏推荐
- c盘使用100%清理方法
- Tidb applicable scenarios
- Upload and modify the use of avatars
- June 24, 2022: golang multiple choice question, what does the following golang code output? A:1; B:3; C:4; D: Compilation failed. package main import ( “fmt“ ) func mai
- Arrays.asList()
- 金仓数据库 KingbaseES 插件DBMS_RANDOM
- 记一次有趣的逻辑SRC挖掘
- Compilation of learning from Wang Shuang (1)
- A random number generator
- 仿真与烧录程序有哪几种方式?(包含常用工具与使用方式)
猜你喜欢

Software testing to avoid being dismissed during the probation period

What are the ways to simulate and burn programs? (including common tools and usage)

Apache ShenYu 入門

FPGA基于VGA显示字符及图片

Ladder Side-Tuning:预训练模型的“过墙梯”

Explanation and use of kotlin syntax for Android

金仓KFS数据级联场景部署
![[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology](/img/82/8cac87231e51698ab17f1274b3a0bd.jpg)
[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology

Open source invites you to participate in the openssf Open Source Security Online Seminar

Technical practice and development trend of video conference all in one machine
随机推荐
ARM64汇编的函数有那些需要注意?
【文件包含漏洞-03】文件包含漏洞的六种利用方式
try-catch-finally
[shangyun boutique] energy saving and efficiency improvement! Accelerating the transformation of "intelligent manufacturing" in the textile industry
GCC related
基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件
Free access to the global human settlements layer (ghsl) dataset from Gee
SQL注入漏洞(繞過篇)
CFCA安心签接入
Kingbasees plug-in ftutilx of Jincang database
Checking whether the double value is an integer - Swift - checking if a double value is an integer - swift
MySQL and Oracle processing CLOB and blob fields
视频会议一体机的技术实践和发展趋势
Network remote access using raspberry pie
How to realize the rich text editor function of mobile terminal
Upload and modify the use of avatars
Software testing to avoid being dismissed during the probation period
Comparator (for arrays.sort)
查询法,中断法实现USART通信
Kingbasees plug-in DBMS of Jincang database_ UTILITY