当前位置:网站首页>子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?
子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?
2022-06-25 10:58:00 【用户9854323】
(1)子类A继承父类B, A a = new A(); 则:
父类B静态代码块->子类A静态代码块->父类B非静态代码块->父类B构造函数->子类A非静态代码块->子类A构造函数
(2)若子类构造函数中显式的调用了父类的某构造函数,则调用该构造函数
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 首先new了一个子类对象,那么就要调用构造方法来初始化该子类对象,但是该类继承自A,所以要先调用父类的构造方法,这里通过super(“B”)显示的调用了父类的带参构造。执行父类的带参构造前要先对父类中的对象进行初始化,对父类中的c成员进行初始化,调用了C类的无参构造,所以调用顺序为: 先调用C类的无参构造 再调用A类的带参构造 最后调用调用子类的构造
(3)在JVM调用mian方法之前先用进行静态内容的初始化。顺序为:父类的静态变量, 父类的静态代码块 ,子类的静态变量,子类的静态代码块。
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
边栏推荐
- Daily 3 questions (3) - check whether integers and their multiples exist
- 金仓数据库 KingbaseES 插件dbms_session
- A random number generator
- 数据库系列:MySQL索引优化总结(综合版)
- 开源社邀请您参加OpenSSF开源安全线上研讨会
- Leetcode 1249. 移除无效的括号(牛逼,终于做出来了)
- [shangyun boutique] energy saving and efficiency improvement! Accelerating the transformation of "intelligent manufacturing" in the textile industry
- 视频会议一体机的技术实践和发展趋势
- Simple use of SVN
- ARM64特有一些的汇编指令
猜你喜欢

COSCon'22 讲师征集令

Advanced single chip microcomputer -- development of PCB (2)

A difficult mathematical problem baffles two mathematicians

Leetcode 1249. 移除无效的括号(牛逼,终于做出来了)

基于SSH的高校实验室物品管理信息系统的设计与实现 论文文档+项目源码及数据库文件

XSS attack

Netease's open source distributed storage system curve officially became the CNCF sandbox project

TASK03|概率论

XSS攻击

Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files
随机推荐
Jincang KFS data cascade scenario deployment
Application of global route guard
2022年PMP项目管理考试敏捷知识点(2)
Previous string inversion topic
What are the functions of arm64 assembly that need attention?
Oracle彻底卸载的完整步骤
每日3题(2)- 找出数组中的幸运数
金仓数据库 KingbaseES 插件dbms_session
垃圾回收机制
Kingbasees plug-in DBMS of Jincang database_ RANDOM
中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析
Technical practice and development trend of video conference all in one machine
【上云精品】节能提效!加速纺织业“智造”转型
Tidb applicable scenarios
Dragon Book tiger Book whale Book gnawing? Try the monkey book with Douban score of 9.5
keep-alive
zabbix分布式系统监控
wait()、notify()和notifyAll()、sleep()、Condition、await()、signal()
[the path of system analyst] Chapter 6: Double inventory demand engineering (comprehensive knowledge concept)
1-7snapshots and clones in VMWare