javaSE初始化顺序的考题

问题:以下代码的运行结果是什么?某大厂的面试题

有一个父类:

public class Father{
	private int i = test();
	private static int j = method();
	
	static{
		System.out.print("(1)");
	}
	Father(){
		System.out.print("(2)");
	}
	{
		System.out.print("(3)");
	}
	
	
	public int test(){
		System.out.print("(4)");
		return 1;
	}
	public static int method(){
		System.out.print("(5)");
		return 1;
	}
}

和一个子类

public class Son extends Father{
	private int i = test();
	private static int j = method();
	static{
		System.out.print("(6)");
	}
	Son(){
//		super();//写或不写都在,在子类构造器中一定会调用父类的构造器
		System.out.print("(7)");
	}
	{
		System.out.print("(8)");
	}
	public int test(){
		System.out.print("(9)");
		return 1;
	}
	public static int method(){
		System.out.print("(10)");
		return 1;
	}
	public static void main(String[] args) {
		Son s1 = new Son();
		System.out.println();
		Son s2 = new Son();
	}
}

运行结果是什么呢?

我们先分析一下,有基础的同学应该知道类的初始化顺序,在这个基础上,我们就知道了结果:

(5)(1)(10)(6)(9)(3)(2)(9)(8)(7)
(9)(3)(2)(9)(8)(7)

分析:

/*
 * 父类的初始化<clinit>:
 * (1)j = method();
 * (2)父类的静态代码块
 * 
 *  父类的实例化方法:
 * (1)super()(最前)
 * (2)i = test();
 * (3)父类的非静态代码块
 * (4)父类的无参构造(最后)
 * 
 * 非静态方法前面其实有一个默认的对象this
 * this在构造器(或<init>)它表示的是正在创建的对象,因为这里是在创建Son对象,所以
 * test()执行的是子类重写的代码(面向对象多态)
 * 
 * 这里i=test()执行的是子类重写的test()方法
 */
/*
 * 子类的初始化<clinit>:
 * (1)j = method();
 * (2)子类的静态代码块
 * 
 * 先初始化父类:(5)(1)
 * 初始化子类:(10)(6)
 * 
 * 子类的实例化方法<init>:
 * (1)super()(最前)      (9)(3)(2)
 * (2)i = test();    (9)
 * (3)子类的非静态代码块    (8)
 * (4)子类的无参构造(最后) (7)
 * 
 * 因为创建了两个Son对象,因此实例化方法<init>执行两次
 * 
 * (9)(3)(2)(9)(8)(7)
 */

子类的super代表父类的整个实例化方法
为什么不会出现(4)?
因为方法的重写问题
父类的实例方法走到第2步时,由于初始化的是子类,因此会调用子类的test()方法

创建了两个Son对象,因此实例初始化方法执行了两次

哪些方法不能被重写:
final方法
静态方法
private等子类中不可见的方法

静态变量与静态代码块是顺序执行的
非静态变量与非静态代码块也是顺序执行的
构造函数总是最后执行

猜你喜欢

转载自blog.csdn.net/fallwind_of_july/article/details/94436213
今日推荐