标签:
关于java中父类子类,成员变量,静态变量,构造器创建的先后顺序,在面试中经常会问到该类型的问题
所以用以下代码进行测试:
public class Test { public static void main(String args[]) { Father f = new Son(); } } class Father { public Father() { System.out.println("Father Construct"); } static int fatherStatic=printFatherStatic(); int father=printFather(); static int printFatherStatic() { System.out.println("print in father static mamber"); return 0; } static int printFather(){ System.out.println("print in father class mamber"); return 0; } } class Son extends Father { public Son() { System.out.println("Son Construct"); } static int sonStatic=printSonStatic(); int son=printSon(); static int printSonStatic() { System.out.println("print in son static mamber"); return 0; } static int printSon(){ System.out.println("print in son class mamber"); return 0; } }
运行结果为:
print in father static mamber
print in son static mamber
print in father class mamber
Father Construct
print in son class mamber
Son Construct
总结一下,得出以下结论:
创建的先后顺序为:父类的静态变量->子类的静态变量->父类的成员变量->父类的构造方法->子类的成员变量->子类的构造方法
总的来说,首先创建静态方法,然后创建父类的成员变量和构造方法,再创建子类的成员变量和构造方法
标签:
原文地址:http://www.cnblogs.com/wentjiang/p/4478005.html