这段小程序对调用对象构造函数时,父类构造函数、成员变量初始化函数,以及非静态初始化块调用顺序进行验证,不考虑静态成员及静态初始化块。
- public class Derive extends Base
- {
- private Member m1 = new Member("Member 1");
- {
- System.out.println("Initial Block()");
- }
- public Derive() {
- System.out.println("Derive()");
- }
- private Member m2 = new Member("Member 2");
- private int i = getInt();
- private int getInt()
- {
- System.out.println("getInt()");
- return 2;
- }
- public static void main(String[] args)
- {
- new Derive();
- }
- }
- class Base
- {
- public Base()
- {
- System.out.println("Base()");
- }
- }
- class Member
- {
- public Member(String m)
- {
- System.out.println("Member() "+m);
- }
- }
- /*程序输出:
- Base()
- Member() Member 1
- Initial Block()
- Member() Member 2
- getInt()
- Derive()
- */
复制代码
|