【JAVA学习路-think in java】p158:生成子类对象时,对基类构造器、静态数据的调用顺序

package pkg;

class UsefulType{
	UsefulType(String str) {
		System.out.println(str);
	}
}

//
class Base_1{
	Base_1(){
		System.out.println("Base_1 constructing");//[5][9]
	}
	static void b1method() {
		System.out.println("b1 method envoking");		
	}
	public UsefulType b1data_pub=new UsefulType("b1_public data generating");
	static UsefulType b1data=new UsefulType("b1 data preparing");//[2]
}
class Base_2{
	Base_2(){
		System.out.println("Base_2 constructing");//[12]
	}
	static void b2method() {
		System.out.println("b2 method envoking");
	}
	public UsefulType b2data_pub=new UsefulType("b2_public data generating");
	static UsefulType b2data=new UsefulType("b2 data preparing");//[11]
}
class Base_3{
	Base_3(){
		System.out.println("Base_3 constructing");//[15]
	}
	static void b3method() {
		System.out.println("b3 method envoking");		
	}
	public UsefulType b3data_pub=new UsefulType("b3_public data generating");
	static UsefulType b3data=new UsefulType("b3 data preparing");//[14]
}

class Ext_1 extends Base_1{
	Ext_1(){
		System.out.println("Extend_1 constructing");//[6]
	}
	static void e1method() {
		System.out.println("e1 method envoking");		
	}
	public UsefulType e1data_pub=new UsefulType("e1_public data generating");
	static UsefulType e1data=new UsefulType("e1 data preparing");//[3]
}

class Ext_2 extends Ext_1{
	Ext_2(){
		System.out.println("Extend_2 contructing");//[7]
	}
	static void e2method() {
		System.out.println("e2 method envoking");		
	}
	public UsefulType e2data_pub=new UsefulType("e2_public data generating");
	static UsefulType e2data=new UsefulType("e2 data preparing");//[4]
}


public class p158 extends Ext_2{
	//data
	private Base_1 b1=new Base_1();//[8]
	private Base_2 b2=new Base_2();//[10]
	private Base_3 b3=new Base_3();//[13]
	
	//own constructor
	public p158() {
		System.out.println("P158 constructing");//[16]
	}
	
	//main
	public static void main(String[] args) {
		new p158();//[1]
	}
}

OUTPUT(right column):

public data is NOT available public data is available
【2】b1 data preparing
【3】e1 data preparing
【4】e2 data preparing
【5】Base_1 constructing
【6】Extend_1 constructing
【7】Extend_2 contructing
【9】Base_1 constructing
【11】b2 data preparing
【12】Base_2 constructing
【14】b3 data preparing
【15】Base_3 constructing
【16】P158 constructing
【2】b1 data preparing
【3】e1 data preparing
【4】e2 data preparing
         b1_public data generating
【5】Base_1 constructing
         e1_public data generating
【6】Extend_1 constructing
         e2_public data generating
【7】Extend_2 contructing
         b1_public data generating
【9】Base_1 constructing
【11】b2 data preparing
         b2_public data generating
【12】Base_2 constructing
【14】b3 data preparing
          b3_public data generating
【15】Base_3 constructing
【16】P158 constructing

总结:

生成子类对象时:

1、最先调用最基类的相关数据,依序到最底层子类。

2、依类的调用次序,初始化所有类的static数据,再依序调用{非static数据、构造器函数}{非static数据、构造器函数};而函数即便是static函数,也不会被调用。

3、static数据只会被初始化一次。非static数据每次均被重新生成。

发布了42 篇原创文章 · 获赞 23 · 访问量 8278

猜你喜欢

转载自blog.csdn.net/Andrew_Zeng/article/details/104182976