Is hiding a member in the base class a bad smell in the code?

爷,独闯天下 提交于 2019-12-11 01:59:50

问题


To me, it sounds more like an exception to hide a member in the base class in order to hack something instead of refactoring a bad design...

Maybe I am wrong. But...

Is it a code smell to hide a member in the base class with 'new' keyword?


回答1:


It depends on the intent. If the purpose is to clarify the concrete return type, for example, then it might not be unreasonable. The classic example here is DbConnection. It is useful that SqlConnection re-declares CreateCommand etc, by returning SqlCommand rather than DbCommand, since SqlCommand has many platform-specific features that would otherwise be a pain. A key point, here, though is that the DbConnection method still works, and the purpose and meaning of the two methods is the same.

If you are doing this and the methods do something different, or return something different, then there are occasions it can cause confusion. And times it can still make sense ;p If they do the same thing, and only vary by formally stating something about the implementation that is useful to the caller, then fine.




回答2:


May be this can help. http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx



来源:https://stackoverflow.com/questions/9462393/is-hiding-a-member-in-the-base-class-a-bad-smell-in-the-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!