本帖最后由 as604049322 于 2014-12-10 12:31 编辑
对象构造过程构造子类之前必须调用父类,且构造块会构造函数之前执行
- public class A {
-
- static{
- System.out.println("A static block");
- }
-
- public A() {
- super();
- System.out.println("A constructor");
- }
-
- {
- System.out.println("A not static block"+this);
- }
- }
复制代码
B类:
- public class B extends A{
- static{
- System.out.println("B static block");
- }
-
- public B() {
- super();
- System.out.println("B constructor");
- }
-
- {
- System.out.println("B not static block");
- }
-
- public static void main(String[] args) {
- new B();
- }
- }
复制代码
结果:
- [*]A static block
- [*]B static block
- [*]A not static blockB@a90653
- [*]A constructor
- [*]B not static block
- [*]B constructor
复制代码
|