简体   繁体   English

覆盖方法只调用父方法 - 有用吗?

[英]Overriding method only calls parent method - useful?

I was looking through an old codebase and I found a method that only calls its parent: 我正在查看旧的代码库,我找到了一个只调用其父代的方法:

@Override
public void select(Object item) {
  super.select(item);
}

Would there be any use case for such a method? 这种方法会有用例吗? For me it looks like I could just remove it. 对我来说,看起来我可以删除它。

Removing it would make almost no difference. 删除它几乎没有任何区别。 You will see a difference when using reflection and looking for the select method on the object. 使用反射并在对象上查找select方法时,您会看到不同之处。 If you ask tell reflection not to look in the object's base class, it's not going to find the method after you delete it. 如果你要求反射不要查看对象的基类,那么在删除它之后它就不会找到该方法。

Yes, this method can be removed without changing the logic of your code. 是的,可以在不更改代码逻辑的情况下删除此方法。

Perhaps it used to have a different implementation which was removed, or was supposed to have a different implementation which was never written. 也许它曾经有一个不同的实现被删除,或者应该有一个从未写过的不同实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM