(1)
构造器调用顺序: 一在类的内部,变量定义的先后顺序决定了初始化的顺序。即使变量定义散布于方法定义之间,它们仍旧会在任何方法(包括构造器)被调用之前得到初始化。 class Tag { Tag(int marker)
{ System.out.println("Tag(" + marker + ")"); } }
class Card { Tag t1 = new Tag(1); // Before constructor Card() { System.out.println("Card()"); t3 = new Tag(33); } Tag t2 = new Tag(2); // After constructor void f() { System.out.println("f()"); } Tag t3 = new Tag(3); // At end }
public class OrderOfInitialization
{ public static void main(String[] args)
{ Card t = new Card(); t.f(); // Shows that construction is done } } 结果应该为
tag(1)
tag(2)
tag(3)
card()
tag(33)
f()