继承关系的类的实例化顺序

父类Allen

public class Allen {
	//静态变量
	private static String  NAME = "Allen Baba";
	
	//实例变量
	private String id = "2017_JJ4";
	
	//静态方法
	static{
		System.out.println("Baba stand_Play");
	}
	
	//实例方法
	{
		System.out.println("Baba move_Play");
	}
	
	//获取name方法
	public String getName() {
		return NAME;
	}
	
	//获取id方法
	public String getId() {
		return id;
	}
	
	public void emmm() {
		System.out.println(NAME);
	}
	Allen() {
		emmm();
		System.out.println("This Constructor is->" + this);
		System.out.println(getName());
		System.out.println(getId());
	}
}

子类LittleAllen

public class LittleAllen extends Allen{
	private static String NAME = "Little Allen";
	private String id = "Little_2017_JJ4";
	
	public static void Little_Stand_Play() {
		System.out.println("Little_Stand_Play");
	}
	
	public void Little_Move_Play() {
		System.out.println("Little_Move_Play");
	}
	
	static {
		System.out.println("Little_Stand_Play");
	}
	
	{
		System.out.println("Little_Move_Play");
	}
	
	public String getName() {
		return this.NAME;
	}
	
	public String getId() {
		return this.id;
	}
	
	public void little_emmmm() {
		System.out.println(NAME);
	}
	public LittleAllen() {
		little_emmmm();
		System.out.println("Little Allen Start~~~~");
		System.out.println(getName());
		System.out.println(getId());
	}
}

主函数Main

public class Main {
	public static void main(String[] args) {
		Allen allen = new LittleAllen();
	}
}

Result:

Baba stand_Play
Little_Stand_Play
Baba move_Play
Allen Baba
This Constructor is->qqttest.LittleAllen@7852e922
Little Allen
null  
Little_Move_Play
Little Allen
Little Allen Start~~~~
Little Allen
Little_2017_JJ4

总结:
实例化(类加载)顺序是:父类静态变量->父类静态方法->子类静态变量->子类静态方法->父类实例变量->父类实例方法->父类构造方法->子类实例变量->子类实例方法->子类构造方法。

说明:
有一个输出结果是null,是因为子类重写了父类的getId()方法,但是实例化顺序时id是private修饰的变量,但不是static静态修饰的,所以此时还不能输出相应的初始化值,只能被系统默认初始化为null。

你可能感兴趣的:(Java)