Should inheritance (of non-interface types) be removed from programming languages?

前端 未结 20 2302
旧时难觅i
旧时难觅i 2020-12-30 05:20

This is quite a controversial topic, and before you say \"no\", is it really, really needed?

I have been programming for about 10 years, and I can\'t honestly sa

20条回答
  •  爱一瞬间的悲伤
    2020-12-30 05:27

    For production code I almost never use inheritance. I go with using interfaces for everything (this helps with testing and improves readability i.e. you can just look at the interface to read the public methods and see what is going on because of well-named methods and class names). Pretty much the only time I would use inheritance would be because a third party library demands it. Using interfaces, I would get the same effect but I would mimic inheritance by using 'delegation'.

    For me, not only is this more readable but it is much more testable and also makes refactoring a whole lot easier.

    The only time I can think of that I would use inheritance in testing would be to create my own specific TestCases used to differentiate between types of tests I have in my system.

    So I probably wouldn't get rid of it but I choose not to use it as much as possible for the reasons mentioned above.

提交回复
热议问题