构造器中绝对绝对不能调用可被覆盖的方法

代码

看下面代码示例。思考程序的输出内容

import java.time.Instant;

class Super{
    public Super(){
        overrideMe();
    }
    public void overrideMe(){

    }
}
public class Student extends Super{

    private final Instant instant;

    Student(){
        instant = Instant.now();
    }

    @Override
    public void overrideMe(){
        System.out.println(instant);
    }

    public static void main(String[] args){
        Student stu = new Student();
        stu.overrideMe();
    }
}

程序的运行结果是:

null
2020-05-13T02:51:01.398Z

思考:为什么两次调用 overrideMe() 方法只有第二次成功输出了instant对象

分析

首先我们分析一下代码,代码中有两个类,Super 类和 Student 类,Super 类中有一个 overrideMe()方法,方法体为空,Super 类的构造器中调用了 overrideMe()方法。Student 继承了 Super 类,Student 类中有一个 Instant 类型的对象,在 Student 的构造器中被实例化,同时 Student 类覆盖了 Super 类的 overrideMe()方法,在方法体中打印 instant 对象。我们在 main 函数中通过调用 Student 的构造器创建了 stu 对象,并且调用其overrideMe()方法。

继续详细的分析代码↓↓

先从main函数下手

public static void main(String[] args){
    Student stu = new Student();
    stu.overrideMe();
}

在main方法中我们通过 Student stu = new Student() 调用 Student 的构造器,由于 Student 类继承了 Super 类而 调用子类构造器之前会先调用超类的构造器,也就是在调用 Student 类的构造器前会先调用 Super 类的构造器↓↓

public Super(){
	overrideMe();
}

在超类构造器中,又调用了 overrideMe()方法,此方法在 Super 中的方法体内容为空,但是由于 overrideMe()方法在 Student 中被覆盖了,实际上 Super 类构造器调用的是 Student 类中 overrideMe()方法↓↓

@Override
public void overrideMe(){
    System.out.println(instant);
}

此方法打印 Student 中 instant 对象,但是此时 Student 的构造函数并没有被调用过,所以此此时 instant 对象实际为 null,这也就是程序运行结果中第一行打印 null 的原因。调用完此方法后,程序回到 Super 类构造器中,超类构造器执行完成,此时才会调用子类(Student)的构造器↓↓

Student(){
    instant = Instant.now();
}

在 Student 的构造方法中通过调用 Instant.now() 方法(获取当前时间戳),实例化了 Student 中的 instant 对象,至此 Student 实例化完成,回到了main 方法↓↓

public static void main(String[] args){
    Student stu = new Student();
    stu.overrideMe();
}

实例化 Student 完成之后我们又调用了其 overrideMe() 方法↓↓

@Override
public void overrideMe(){
    System.out.println(instant);
}

此时,instant 对象在刚才的 Student 实例化时也被实例化了,此时打印的结果就是我们在实例化时获取到的时间戳了,这就是第二行输出结果

回到主题,构造器中不能调用可被覆盖的方法,我们稍微改造一下 Student 中的 overrideMe()方法

@Override
public void overrideMe(){
	System.out.println(instant.getNano());
}

通过调用 instant 对象的 getNano()方法,我们意图获取时间戳的纳秒表示。通过上面的分析,在第一次调用overrideMe()方法时,instant对象为 null,所以我们可以获得下面的输出结果:

Exception in thread "main" java.lang.NullPointerException
        at Student.overrideMe(Student.java:21)
        at Super.<init>(Student.java:5)
        at Student.<init>(Student.java:15)
        at Student.main(Student.java:25)

没错肯定会出现空指针异常!

这也就是为什构造器中不能调用可被覆盖的方法,因为对于可覆盖方法,在子类覆盖过程中,很有可能会使用到子类中特有的属性,而由于实例化顺序的原因,在父类中调用被覆盖了的方法,就很有可能会造成程序运行失败。

注:通过构造器调用私有方法final 方法静态方法是安全的,因为这些方法都不能被覆盖

posted @ 2020-05-13 11:34  UtilMan  阅读(380)  评论(0编辑  收藏  举报