当我们使用与父私有(private)方法相同的名称时,Sonar 提示类中的私有(private)方法名称。在代码质量上定义一个与父私有(private)方法同名的私有(private)方法有什么缺点?
或者我们是否需要将其归类为误报
最佳答案
在我看来,这是因为这可能会造成混淆。考虑下面,阅读评论:
class Child extends Super{
public void myMethod() {
System.out.println("in child");
}
}
class Super{
public static void main(String[] args) {
Super s = new Child();
s.myMethod(); // At this point you might expect myMethod of child to be called if it'll call the Parent's since it is private.
}
private void myMethod() {
System.out.println("in super");
}
}
关于java - Sonar 重命名此方法;父类中有一个 "private"方法同名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54150765/