Java类加载顺序
---Person.java
public class Person { private static String name ="Horld"; private int age=1; //静态、成员变量需在静态、代码块之前声明; static{ System.out.println("父类静态成员变量 name is " +name); System.out.println("父类静态代码块执行"); } { System.out.println("父类成员变量age执行 is " + age); System.out.println("父类代码块执行"); } public Person(){ System.out.println("Person-父类构造函数执行"); setAge(20);//若子类没有重写该函数,则调用父类函数 } public void setAge(int Age){ System.out.println("父类成员方法执行Age is " + Age); this.age = Age; } } class Man extends Person{ private int mge = 18; static{ System.out.println("子类静态代码块执行"); } { System.out.println("代码块子类成员变量mge执行 is " + mge); System.out.println("子类代码块执行"); } public Man(){ System.out.println("构造函数子类成员变量mge执行 is " + mge); System.out.println("Man-子类构造函数执行"); } @Override//可不用注解 public void setAge(int Age){ System.out.println("子类成员方法执行Age is " + Age); System.out.println("子类成员变量mge is " + mge); this.mge = Age; } public void getMge(){ System.out.println("子类成员变量mge is " + mge); } }
--App.java
public class App { public static void main(String[] args) throws Exception { System.out.println("Hello, World!"); Man p = new Man(); System.out.println("函数构造完成"); p.getMge(); } }
--执行App显示结果: