当前位置:网站首页>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
边栏推荐
- Open source invites you to participate in the openssf Open Source Security Online Seminar
- ARM64汇编的函数有那些需要注意?
- 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
- Explanation and use of kotlin syntax for Android
- 牛客网:旋转数组
- MySQL synchronous data configuration and shell script implementation
- 数据库系列:MySQL索引优化总结(综合版)
- [file containing vulnerability-03] six ways to exploit file containing vulnerabilities
- 基于SSH的高校实验室物品管理信息系统的设计与实现 论文文档+项目源码及数据库文件
- 今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
猜你喜欢

Double buffer transparent encryption and decryption driven course paper + project source code based on minifilter framework

SystemVerilog (XIII) - enumerate data types

网络远程访问的方式使用树莓派

Apache ShenYu 入門

Use of three-level linkage plug-ins selected by provinces and cities

【文件包含漏洞-03】文件包含漏洞的六种利用方式

基于Minifilter框架的双缓冲透明加解密驱动 课程论文+项目源码
![[file containing vulnerability-03] six ways to exploit file containing vulnerabilities](/img/4f/495c852eb0e634c58e576d911a2c14.png)
[file containing vulnerability-03] six ways to exploit file containing vulnerabilities

寿命分布 4种

视频会议一体机的技术实践和发展趋势
随机推荐
Vulnérabilité à l'injection SQL (contournement)
Causes and solutions of over fitting
SystemVerilog (XIII) - enumerate data types
Leetcode 1249. Remove invalid brackets (awesome, finally made)
如何实现移动端富文本编辑器功能
Spannable 和 Editable、SpannableString 和 SpannableString
每日3题(3)-检查整数及其两倍数是否存在
Android:kotlin中Gson与JSON的泛型映射解析
一个数学难题,难倒两位数学家
Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files
A program reflecting the characteristics of C language program structure
Free access to the global human settlements layer (ghsl) dataset from Gee
JVM 原理简介
开源社邀请您参加OpenSSF开源安全线上研讨会
贝叶斯
记一次有趣的逻辑SRC挖掘
Crawler scheduling framework of scratch+scratch+grammar
Kingbasees plug-in DBMS of Jincang database_ session
基于Minifilter框架的双缓冲透明加解密驱动 课程论文+项目源码
Introduction to socket UDP and TCP