【 tulaoshi.com - 编程语言 】
                             
                            类的继续机制使得子类可以使用父类的功能(即代码),并且子类也具有父类的类型。下面介绍类在继续关系上的初始化的顺序问题。   
  示例1:   
   
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)   
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)   
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)   
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)class SuperClass 
  { 
  SuperClass() 
  { 
  System.out.println("SuperClass constrUCtor"); 
  } 
  } 
  public class SubClass extends SuperClass 
  { 
  SubClass() 
  { 
  System.out.println("SubClass constructor"); 
  } 
  public static void main(String[] args) 
  { 
  SubClass sub = new SubClass(); 
  } 
  } 
  输出结果: SuperClass 
  constructor 
  SubClass constructor 
  在子类中只实例化了一个子类对象。从输出结果上看,程序并不是一开始就运行自己的构造方法,而是先运行其父类的默认构造方法。注重:程序自动调用其父类的默认构造方法。   
  示例2:   
  class SuperClass 
  { 
  SuperClass(String str) 
  { 
  System.out.println("Super with a string."); 
  } 
  } 
  public class SubClass extends SuperClass 
  { 
  SubClass(String str) 
  { 
  System.out.println("Sub with a string."); 
  } 
  public static void main(String[] args) 
  { 
  SubClass sub = new SubClass("sub"); 
  } 
  } 
  在JDK下编译此程序不能成功。正如上例中说的:程序在初始化子类时先要寻找其父类的默认构造方法,结果没找到,那么编译自然不能通过。   
  解决这个问题有两个办法:   
  1.在父类中增加一个默认构造方法。 
  2.在子类的构造方法中增加一条语句:super(str); 且必须在第一句。   
  这两种方法都能使此程序通过编译,但就本程序来说运行结果却不相同。   
  第1种方法的运行结果是:  
  Sub with a string.   
  第2种方法的运行结果是:   
  Super with a string. 
  Sub with a string. 
  第2种解决方法实际上是指定编译器不要寻找父类的默认构造方法,而是去寻找带一个字符串为参数的构造方法。   
     下面介绍对象的初始化顺序问题。   
  示例3:   
  class One 
  { 
  One(String str) 
  { 
  System.out.println(str); 
  } 
  } 
  class Two 
  { 
  One one_1 = new One("one-1"); 
  One one_2 = new One("one-2"); 
  One one_3 = new One("one-3"); 
  Two(String str) 
  { 
  System.out.println(str); 
  } 
  } 
  public class Test 
  { 
  public static void main(String[] args) 
  { 
  System.out.println("Test main() start..."); 
  Two two = new Two("two"); 
  } 
  } 
  输出结果: 
  Test main() start... 
  one-1 
  one-2 
  one-3 
  two 
  在main()方法中实例化了一个Two类的对象。但程序在初始化Two类的对象时,并非先调用Two类的构造方法,而是先初始化Two类的成员变量。这里Two类有3个成员变量,它们都是One类的对象,所以要先调用3次One类的相应的构造方法。最后在初始化Two类的对象。   
  示例4:   
  class One   
  { 
  One(String str) 
  { 
  System.out.println(str); 
  } 
  } 
  class Two 
  { 
  One one_1 = new One("one-1"); 
  One one_2 = new One("one-2"); 
  static One one_3 = new One("one-3"); 
  Two(String str) 
  { 
  System.out.println(str); 
  } 
  } 
  public class Test 
  { 
  public static void main(String[] args) 
  { 
  System.out.println("Test main() start..."); 
  Two two_1 = new Two("two-1"); 
  System.out.println("------------"); 
  Two two_2 = new Two("two-2"); 
  } 
  } 
  输出结果: 
  Test main() start... 
  one-3 
  one-1 
  one-2 
  two-1 
  ------------ 
  one-1 
  one-2 
  two-2 
  假如一个类中有静态对象,那么它会在非静态对象前初始化,但只初始化一次。非静态对象每次调用时都要初始化。