为什么大多数编程语言不支持多重继承?

发布于 2024-09-02 05:05:22 字数 59 浏览 0 评论 0原文

为什么大多数编程语言不支持多重继承?

我真的可以使用这个功能来开发不同布局的应用程序吗?

Why is multiple inheritance not supported in most of programming language?

I could really use this feature to develop different layout of application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柏拉图鍀咏恒 2024-09-09 05:05:22

作为开发人员,多重继承在许多情况下都很有用,但它极大地增加了语言的复杂性,这使编译器开发人员和程序员的生活变得更加困难。

  • 当两个父类具有相同名称的数据成员或方法时,就会出现问题。很难确定子类正在引用哪一个。

  • 另一种情况发生在两个父类继承同一个基类时,在继承层次结构中形成“钻石”模式。

  • 需要指定父类的初始化/细化的顺序 - 当继承的顺序发生变化时,这有时会导致行为发生变化 - 某些事情可能会让开发人员感到惊讶。

  • 某些语言支持对“super”或等效项的引用,它引用该对象的基类的属性。这在具有多重继承的语言中变得难以支持。

  • 某些语言尝试提供自动对象关系模型,因此可以使用常规 RDMS 使对象持久化。即使在最好的情况下,这种映射也是困难的(它被描述为软件开发的“越南战争”),但如果支持多重继承,则困难得多。

Multiple inheritance is useful in many situations as a developer, but it greatly increases the complexity of the language, which makes life harder for both the compiler developers and the programmers.

  • One problem occurs when two parent classes have data members or methods of the same name. It is difficult to resolve which is being referenced by the sub-class.

  • Another occurs when two parent classes inherit from the same base class, forming a "diamond" pattern in the inheritance hierarchy.

  • The order that the initialisation/elaboration of the parent classes needs to be specified - this can sometimes lead to behaviour changing when the order of the inheritance changes - something may catch developers by surprise.

  • Some languages support a reference to 'super', or equivalent, which refers to an attribute of the base class for this object. That becomes difficult to support in a language with multiple inheritance.

  • Some languages attempt to provide an automatic Object-Relational Model, so the objects can be made persistent with a regular RDMS. This mapping is difficult at the best of times (it has been described as the "Vietnam War" of software development), but it is much more difficult if multiple inheritance is supported.

逆蝶 2024-09-09 05:05:22

不支持它的原因之一是方法解析的模糊性。

http://en.wikipedia.org/wiki/Diamond_problem

但是,我不确定“大多数”编程语言是什么意思。目前使用的许多工具都直接支持它(C++、Python、Perl、OCaml)或具有类似功能的机制(例如 Ruby 和 Scala)。

One reason not to support it is ambiguity of method resolution.

http://en.wikipedia.org/wiki/Diamond_problem

However, I'm not sure what you mean by "most" programming languages. Many that are in use today support it directly (C++, Python, Perl, OCaml) or have a mechanism for similar functionality (Ruby and Scala come to mind).

別甾虛僞 2024-09-09 05:05:22

许多语言不支持多重继承的真正原因是语言开发人员的懒惰。为了掩盖这个尴尬的失败,各种借口都找了出来,“这让开发者日子不好过”等等,但对于任何真正使用过实现良好的语言的人来说,大约1个月后,多重继承就变得自然而容易了。没什么大不了的。

它唯一的问题是,当你意识到它是多么有用和简单之后,你往往会对不支持它的语言过敏,这可能会限制你的职业前景。

所以我的建议是远离它。

The real reason why multiple inheritance is not supported across many langauges, is just the laziness of language developers. To cover up this embarrassing failure, all sorts of excuses are made, "it makes life difficult for the developer" bla bla, but for anyone who has actually used a language that implements it well, multiple inheritance becomes natural and easy after about 1 month. No big deal.

The only problem with it is after you have realized how useful and easy it is, you tend to become allergic to languages that don't support it and this may constrain your career prospects.

So my advice would be to stay well away from it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文