java对象的构造顺序(珠海20090326)

package com.constructor.test;

class Parent {
	int pm1;
	int pm2 = 100;
	int pm3 = pmethod();
	{
		System.out.println("Parent's instance initialize block");
	}
	public static int spm1 = 200;
	public static int spmethod = spmethod();
	static {
		System.out.println("Parent's static initialize block");
	}

	Parent() {
		System.out.println("Parent's default constructor");
	}

	Parent(int i,int j)
	{
		//无用this();就不会调用默认的构造器
		System.out.println("two 参数的parent构造器");
	}
	
	static void staticmethod() {
		System.out.println("Parent's staticmethod");
	}

	int pmethod() {
		System.out.println("Parent's method");
		return 3;
	}
	static int spmethod()
	{
		System.out.println("parent's static field and static method give it the value");
		return 88;
	}
}

class Child extends Parent {
	int cm1;
	int cm2 = 1000;
	int cm3 = cmethod();
	Other co;
	static Other co2;
	public static int cmethod = scmethod();
	public static int scm1 = 2000;
	{
		System.out.println("Child's instance initialize block");
	}
	static {
		co2 = new Other();
		cmethod2(); // 要static的方法才行
		System.out.println("Child's static initialize block");
	}
	{
		System.out.println("Child's instance initialize block 2222");
	}

	Child() {
		co = new Other();
		System.out.println("Child's default constructor");
	}

	Child(int m) {
		this();   //如果无this就不会调会默认的构造器Child()
		cm1 = m;
		System.out.println("Child's self-define constructor");
	}

	static void staticmethod() {
		System.out.println("Child's staticmethod");
	}

	int cmethod() {
		System.out.println("Child's method");
		return 3;
	}

	static void cmethod2() {
		System.out.println("Child's method2!!!");
	}
	static int scmethod()
	{
		System.out.println("child's static field and static method give it the value");
		return 888;
	}

}

class Other {
	int om1;

	Other() {
		System.out.println("Other's default constructor");
	}

}

public class TestConstructor {
	public static void main(String args[]) {
		Child c;
		System.out.println("program start");
		System.out.println("Child.scm1=" + Child.scm1);
		// System.out.println("c.cm3="+c.pm2); pm2还未被初始化!
		c = new Child(10);
		System.out.println("program end");
		System.out.println();
		System.out.println("second instance begin");
		Child d = new Child(20);
		System.out.println("sencond instance end");
		//Child e = new Child(10,12); //没有声明Child(int,int)的构构器,不能通过编译
		Parent e = new Parent(10,12);
		
	}
}


program start
parent's static field and static method give it the value
Parent's static initialize block
child's static field and static method give it the value
Other's default constructor
Child's method2!!!
Child's static initialize block
Child.scm1=2000
Parent's method
Parent's instance initialize block
Parent's default constructor
Child's method
Child's instance initialize block
Child's instance initialize block 2222
Other's default constructor
Child's default constructor
Child's self-define constructor
program end

second instance begin
Parent's method
Parent's instance initialize block
Parent's default constructor
Child's method
Child's instance initialize block
Child's instance initialize block 2222
Other's default constructor
Child's default constructor
Child's self-define constructor
sencond instance end
Parent's method
Parent's instance initialize block
two 参数的parent构造器


从代码可以看到,对象的构造分为 类的构造实例的构造两部分.
类的构造是在类的静态属性,方法调用之前完成的.System.out.println("Child.scm1=" + Child.scm1);之前就会完成类的静态属性和方法和块.
实例的构造是在new 构造方法时开始做的,Child d = new Child(20);.

构造的详细顺序如下:

类构造 (只做一次的,不论你new了多少个对象)

1.父类的static 属性(field)
2.父类的static块(block)
3.子类的static field
4.子类的static block

实例构造 (new一次做一次)

1.实例父类的field (非static)
2.实例父类的初始化block (非static,是用{}括起来的那些)
3.实例父类的默认构造器 (默认构造器是类名()这个)
4.实例子类(这个类)的field
5.实例子类(这个类)的初始化block
6.实例子类(这个类)的默认构造器 /no 应该是new的那一个构造器,见Parent e = new Parent(10,12);

(1)属性的构造顺序是按顺序的,先出现先构造.
(2)块的构造顺序是也是按顺序的,先出现先构造.
(3)如果块中,或方法中有new X类();的,就会调用X类的构造器.
(4)final不能用来声明构造方法.

你可能感兴趣的:(java,C++,c,C#,J#)