public class X{
Y y = new Y(); //4:实例化前先初始化所有的属性,Y ->执行Y()
static{
System.out.println("tttt"); //1:首先找到入口main方法->在X类中->加载X类->执行static属性和语句->输出‘tttt’
}
X(){
System.out.println("X"); //6:实例化X->输出‘X’->实例化子类Z
}
public static void main(String[] args){
new Z(); //2:要实例化Z先加载Z类->加载Z类前先加载父类X,但是X已经加载过了
}
}
class Y{
Y(){
System.out.println("Y"); //5:执行Y()->输出‘Y’->再执行X()
}
}
class Z extends X{
Y y = new Y(); //7:实例化Z前,先初始化Z的属性,Y->执行Y()->输出‘Y’
static{
System.out.println("tt"); //3:加载Z类->执行static属性和语句->输出‘tt’->再去实例化Z->想要实例化Z,先实例化父类X
}
Z(){
System.out.println("Z"); //8:最后实例化Z->执行Z()->输出‘Z’
}
}
java static
最新推荐文章于 2024-03-25 22:32:23 发布