构造函数的作用细节

class Window {
	Window(int marker) { System.out.println("Window(" + marker + ")"); }
}
class House {
	Window w1 = new Window(1); 
	House() {
		System.out.println("House()");
		w3 = new Window(33); 
	}
	Window w2 = new Window(2); 
	void f() {
		System.out.println("f()");
	}
	static Window w3 = new Window(3); 
}

public class Est {
	public static void main(String[] args) {
		House h = new House();
		h.f(); 
	}
}

输出结果
Window(3)//因为 是定义了一个静态对象早已初始化所以最先运行
Window(2)//构造方法初始化先初始化成员变量再运行方法语句
Window(1)
House()//最后再运行构造方法语句。
Window(33)
f()

猜你喜欢

转载自blog.csdn.net/qq_43813373/article/details/88619420