Ensure that constructors do not call overridable methods

环境

jdk:1.8+

前言

今天在跑PMD验证代码时,报了个错误:constructors do not call overridable methods

具体来说就是不建议在构造方法里面去调用可以被重载的方法;
为什么呢?

这里我翻译一篇老外的文章;

示例代码

现在假设我们有如下这样的代码:

class SuperClass {
  public SuperClass () {
    doLogic();
  }
 
  public void doLogic() {
    System.out.println("This is superclass!");
  }
}
 
class SubClass extends SuperClass {
  private String color = null;
  public SubClass() {
    super();
    color = "Red";
  }
 
  public void doLogic() {
    System.out.println("This is subclass! The color is :" + color);
    // ...
  }
}
 
public class Overridable {
  public static void main(String[] args) {
    SuperClass bc = new SuperClass();
    // Prints "This is superclass!"
    SuperClass sc = new SubClass();
    // Prints "This is subclass! The color is :null"
  }
}

运行后,会发现new SubClass()打印出来的colornull
分析代码,我们知道,new SuperClass()这个调用的是父类本身的doLogic()方法,没有问题;
当执行到new SubClass()方法时,调用父类构造方法,但是执行doLogic()方法时,会去调子类的;
而这时,子类的构造方法并没有执行完毕,也就是并没有执行color = "Red"

这也就是为什么PMD检测代码时,不建议我们在构造方法里面调用可被重载方法的原因~~~
解决办法:在父类中,把doLogic()方法变为不可被重载即可:

public final void doLogic() {
    System.out.println("This is superclass!");
  }

老外自己的解释:

The doLogic() method is invoked from the superclass’s constructor. When the superclass is constructed directly, the doLogic() method in the superclass is invoked and executes successfully. However, when the subclass initiates the superclass’s construction, the subclass’s doLogic() method is invoked instead. In this case, the value of color is still null because the subclass’s constructor has not yet concluded.

我自己的翻译:

doLogic()方法是在超类构造函数中调用的。直接构造超类时,超类中的doLogic()方法将被调用并成功执行。但是,当子类启动超类的构造时,将改为调用子类的doLogic()方法。在这种情况下,由于子类的构造函数尚未结束,因此color的值仍为null。

总结

这条PMD规则,倒是让我对final又多了一层的理解;

翻译地址:

MET05-J. Ensure that constructors do not call overridable methods

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山鬼谣me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值