主要想探索一下java类的初始化过程。以及父类指向子类时候的调用值。
废话不说,直接上代码。
父类:
public class Parent {
public String name;
public int age;
public static String country = staticShow();
public static String address;
public Parent(){
System.out.println("父类构造函数"+"age:" + age + " ,name: " + name +"country:" + country + " , address:" +address);
age=20;
name="parent_second";
address="上海";
System.out.println("父类构造函数age:" + age + " ,name: " + name +"country:" + country + " , address:" +address);
}
{
System.out.println("-----------父类构造代码块parent start-------");
System.out.println("默认值:"+"age:" + age + " ,name: " +name);
age=10;
name="parent_first";
System.out.println("显示初始化后"+"age:" + age + " ,name: " +name);
System.out.println("-----------父类构造代码块parent end-------");
}
static {
System.out.println("-----------父类静态代码块parent static start-------");
System.out.println("country:" + country + " ,默认值: address:" +address);
address = "shanghai";
System.out.println("country:" + country + " ,初始化: address:" +address);
System.out.println("-----------父类静态代码块parent static end-------");
}
public void show() {
System.out.println("父类show方法"+"age:" + age + " ,name: " + name +"country:" + country + " , address:" +address);
}
public static String staticShow(){
System.out.println("-----------父类静态方法parent static start-------");
System.out.println("默认值:country:" + country + " , address:" +address);
country = "china";
System.out.println("初始化:country:" + country + " , address:" +address);
System.out.println("-----------父类静态方法parent static end-------");
return country;
}
}
子类:
public class Son extends Parent {
public String name;
public int age;
public static String country = staticShow();
public static String address;
public Son(){
System.out.println("=================子类构造函数son show() start================================"
+ " age:" + age + " ,name: " + name +"country:" + country + " , address:" +address);
age=40;
name="son_second";
System.out.println(" age:" + age + " ,name: " + name +"country:" + country + " , address:" +address
+"=======================子类构造函数son show() end==============================");
}
{
System.out.println("-----------子类构造代码块son start-------");
System.out.println("默认值" + "age:" + age + " ,name: " +name);
age=30;
name="son_first";
System.out.println("显示初始化:age:" + age + " ,name: " +name);
System.out.println("-----------子类构造代码块son end-------");
}
static {
System.out.println("-----------子类静态son static start-------");
System.out.println("country:" + country + " ,默认值: address:" +address);
address = "son_shanghai";
System.out.println("country:" + country + " ,初始化: address:" +address);
System.out.println("-----------子类静态son static end-------");
}
public void show() {
System.out.println("=================子类son show() start================================"
+ " age:" + age + " ,name: " + name +"country:" + country + " , address:" +address
+"=======================子类son show() end==============================");
}
public static String staticShow(){
System.out.println("-----------子类静态方法son static start-------");
System.out.println("默认值" + "country:" + country + " , address:" +address);
country = "son_china";
System.out.println("初始化 :country:" + country + " , address:" +address);
System.out.println("-----------子类静态方法son static end-------");
return country;
}
}
测试类:
public class Test {
public static void main(String[] args) {
Parent son2=new Son();
Parent.staticShow();
Son.staticShow();
son2.staticShow();//调用的父类静态方法
son2.show();//调用的是子类
System.out.println();
System.out.println(son2.age+"===" + son2.name+" ===" + son2.country+"===" + son2.address);
}
}
结果:
-----------父类静态方法parent static start-------
默认值:country:null , address:null
初始化:country:china , address:null
-----------父类静态方法parent static end-------
-----------父类静态代码块parent static start-------
country:china ,默认值: address:null
country:china ,初始化: address:shanghai
-----------父类静态代码块parent static end-------
-----------子类静态方法son static start-------
默认值country:null , address:null
初始化 :country:son_china , address:null
-----------子类静态方法son static end-------
-----------子类静态son static start-------
country:son_china ,默认值: address:null
country:son_china ,初始化: address:son_shanghai
-----------子类静态son static end-------
-----------父类构造代码块parent start-------
默认值:age:0 ,name: null
显示初始化后age:10 ,name: parent_first
-----------父类构造代码块parent end-------
父类构造函数age:10 ,name: parent_firstcountry:china , address:shanghai
父类构造函数age:20 ,name: parent_secondcountry:china , address:上海
-----------子类构造代码块son start-------
默认值age:0 ,name: null
显示初始化:age:30 ,name: son_first
-----------子类构造代码块son end-------
=================子类构造函数son show() start================================ age:30 ,name: son_firstcountry:son_china , address:son_shanghai
age:40 ,name: son_secondcountry:son_china , address:son_shanghai=======================子类构造函数son show() end==============================
-----------父类静态方法parent static start-------
默认值:country:china , address:上海
初始化:country:china , address:上海
-----------父类静态方法parent static end-------
-----------子类静态方法son static start-------
默认值country:son_china , address:son_shanghai
初始化 :country:son_china , address:son_shanghai
-----------子类静态方法son static end-------
-----------父类静态方法parent static start-------
默认值:country:china , address:上海
初始化:country:china , address:上海
-----------父类静态方法parent static end-------
=================子类son show() start================================ age:40 ,name: son_secondcountry:son_china , address:son_shanghai=======================子类son show() end==============================
20===parent_second ===china===上海
说明:加载顺序是先父类后子类,先静态再动态
父类静态变量隐式初始化
父类静态变量显示初始化
子类静态变量隐式初始化
子类静态变量显示初始化
父类成员变量隐式初始化
父类成员变量显示初始化
父类构造代码块
父类构造函数执行
子类成员变量隐式初始化
子类成员变量显式初始化
子类构造代码块
子类构造函数执行
静态变量,静态方法,静态代码块在类加载的时候就加载了,所以都是不需要创建对象就可以初始化的。
创建对象的时候先执行父类的变量、构造代码以及构造函数。最后在执行子类的。
父类类型指向子类对象时候:
变量全部是父类的值。静态方法调用父类的。非静态方法调用子类的。父类不能调用子类特有的方法。