真的需要继承吗? [英] Is Inheritance really needed?

查看:81
本文介绍了真的需要继承吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须承认我有点像OOP怀疑论者。面向对象的不良教学和实验经验无济于事。所以我在Visual Basic中变成了一个狂热的信徒(经典的!)。

I must confess I'm somewhat of an OOP skeptic. Bad pedagogical and laboral experiences with object orientation didn't help. So I converted into a fervent believer in Visual Basic (the classic one!).

然后有一天我发现C ++已经改变了,现在有了STL和模板。我真的很喜欢!使语言有用。然后另一天MS决定对VB进行面部手术,我真的很讨厌无端变化的最终结果(使用end while代替wend会让我成为一个更好的开发者?为什么不放下nextfor结束了,为什么呢?为什么强迫吸气者与安装者一起?等等?加上如此多的Java功能,我觉得这些功能毫无用处(例如继承,以及分层框架的概念)。

Then one day I found out C++ had changed and now had the STL and templates. I really liked that! Made the language useful. Then another day MS decided to apply facial surgery to VB, and I really hated the end result for the gratuitous changes (using "end while" instead of "wend" will make me into a better developer? Why not drop "next" for "end for", too? Why force the getter alongside the setter? Etc.) plus so much Java features which I found useless (inheritance, for instance, and the concept of a hierarchical framework).

现在,几年后,我发现自己在问这个哲学问题:是否需要继承真正

And now, several years afterwards, I find myself asking this philosophical question: Is inheritance really needed?

该团伙四分之一说我们应该支持对象组合而不是继承。在考虑之后,我找不到你可以用继承做的事情,你不能用对象聚合和接口做。所以我想知道,为什么我们一开始就拥有它?

The gang-of-four say we should favor object composition over inheritance. And after thinking of it, I cannot find something you can do with inheritance you cannot do with object aggregation plus interfaces. So I'm wondering, why do we even have it in the first place?

任何想法?我很想看到一个例子,说明绝对需要继承的地方,或者使用继承而不是组合+接口可以导致更简单和更容易修改的设计。在以前的工作中,我发现如果你需要更改基类,你还需要修改几乎所有的派生类,因为它们取决于父的行为。如果你让基类的方法变得虚拟......那么就不会发生太多的代码共享:(

Any ideas? I'd love to see an example of where inheritance would be definitely needed, or where using inheritance instead of composition+interfaces can lead to a simpler and easier to modify design. In former jobs I've found if you need to change the base class, you need to modify also almost all the derived classes for they depended on the behaviour of parent. And if you make the base class' methods virtual... then not much code sharing takes place :(

另外,当我最终创建自己的编程语言时(很长一段时间)我发现大多数开发人员没有满足的愿望),我认为添加继承没有任何意义......

Else, when I finally create my own programming language (a long unfulfilled desire I've found most developers share), I'd see no point in adding inheritance to it...

推荐答案

真的很简短的答案:不需要继承因为只需要字节代码。但显然,字节代码或汇编不是编写程序的实际方法.OOP不是编程的唯一范例。但是,我离题了。

Really really short answer: No. Inheritance is not needed because only byte code is truly needed. But obviously, byte code or assemble is not a practically way to write your program. OOP is not the only paradigm for programming. But, I digress.

我在21世纪初进入计算机科学专业,当时继承(是a),作文(有a)和接口(做a)是平等的因此,我使用很少的继承,因为它通常更适合于组合。这是强调的,因为许多教授因为滥用继承而看到了错误的代码(以及你所描述的)。

I went to college for computer science in the early 2000s when inheritance (is a), compositions (has a), and interfaces (does a) were taught on an equal footing. Because of this, I use very little inheritance because it is often suited better by composition. This was stressed because many of the professors had seen bad code (along with what you have described) because of abuse of inheritance.

无论是否使用遗传创建语言,您是否可以创建一种编程语言来防止不良习惯和糟糕的设计决策?

这篇关于真的需要继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆