块,静态块,子类,父类,继承加载顺序
来源:互联网 发布:狸窝视频剪辑软件下载 编辑:程序博客网 时间:2024/06/13 01:34
- class parent{
- {
- System.out.println("(parent)Loading the block...");
- }
- static
- {
- System.out.println("(parent)Loading the static block...");
- }
- public parent()
- {
- System.out.println("(parent)Loading the construct...");
- }
- }
- class child extends parent
- {
- {
- System.out.println("(child)Loading the block...");
- }
- static
- {
- System.out.println("(child)Loading the static block...");
- }
- public child()
- {
- System.out.println("(child)Loading the construct...");
- }
- }
- class load{
- public static void main(String ar[])
- {
- //new parent();
- new child();
- }
- }
输出结果是:
(parent)Loading the static block...
(child)Loading the static block...
(parent)Loading the block...
(parent)Loading the construct...
(child)Loading the block...
(child)Loading the construct...
事实并非想象中:
(parent)Loading the static block...
(parent)Loading the block...
(parent)Loading the construct...
(child)Loading the static block...
(child)Loading the block...
(child)Loading the construct...
分析:
过程静态构造块>构造块>构造方法
由于继承是要先加载父类,再加载子类
而static 是与类中共存的所以一开始生成对象时,必先先执行静态方法,因此会先打印
(parent)Loading the static block...
(child)Loading the static block...
然后构造块是先构造方法,而子类中默认调用了super()方法,所以会打印
(parent)Loading the block...
(parent)Loading the construct...
最后打印
(child)Loading the block...
(child)Loading the construct...
分析到此,不知道理解有没错!
- 块,静态块,子类,父类,继承加载顺序
- Java继承中父类子类构造方法、静态代码块、非静态代码块的执行顺序
- 当子类继承父类时,构造块,静态块,静态方法的执行顺序
- JAVA中父类与子类静态代码块、非静态代码块、构造函数的加载顺序
- Java子类继承父类,静态代码块、普通代码块、构造代码块的执行顺序
- java父类子类中静态块非静态块构造方法的执行顺序和继承
- 静态代码块、代码块、构造方法、普通方法&父类、子类顺序
- 静态块,普通代码块,构造方法,父类、子类的初始化顺序
- 父类 子类 动态代码块 静态代码块 构造函数 执行顺序
- 子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?
- 子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?
- 静态属性静态代码块加载顺序
- 静态代码块与非静态代码块加载顺序
- 关于Java中的静态块,非静态块,子类父类的静态块,非静态块的执行顺序
- 关于Java中的静态块,非静态块,子类父类的静态块,非静态块的执行顺序
- 坑【2】父类子类以及静态块,方法中的加载顺序
- JAVA基础:子类、父类继承关系 静态块、非静态块、构造函数初始化顺序性、静态非静态方法覆盖
- java继承中父类和子类静态、非静态代码块,构造函数,静态方法的执行顺序
- 连接
- 管理思考
- Endpoint Security
- Microsoft Expression Web: Visual QuickStart Guide
- WinForm数据绑定-理解数据绑定机制
- 块,静态块,子类,父类,继承加载顺序
- Professional Windows Workflow Foundation
- JXTA介绍【转摘】
- pascal语言实现二叉排序树
- Alan Simpson's Windows Vista Bible, Desktop Edition
- 一些基本的面试题目
- The Definitive Guide to Grails
- 总结的网站开发的要点
- Ajax and REST Recipes: A Problem-Solution Approach