C++ 中的多重继承与Java

发布于 2024-12-10 18:01:20 字数 226 浏览 0 评论 0原文

当我发现Java中不包含多重继承的原因时,给出的原因是保持“简单性”和“减少复杂性”。 然而,在来自 C++ 背景的 Java 环境中工作,您是否不认为支持多重继承的接口概念使问题变得复杂而不是解决问题?这是否会导致这样的推论:

  1. 我们一定不能在Java中使用多重继承,并且我们的代码架构应该相应地设计?
  2. 使用接口的概念进行多重继承,我认为与 st 相比不太有利(至少对我来说)

When i came across the reason why Multiple Inheritance was not included in Java, the reasons given were to keep 'simplicity' and 'reduce complication'.
However working with Java environment coming from a C++ background, don't you think that Interface concept to support multi inheritance has complicated the matter rather than solving it? Does it lead to the inference that:

  1. We must NOT use multiple inheritance in Java, and our code architecture should be designed accordingly?
  2. Use concept of Interface for multiple inheritance, which i think is less favorable (atleast for me) compared to the st

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

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

发布评论

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

评论(4

聊慰 2024-12-17 18:01:20

您应该阅读 Bjarne Stroustrup 关于多重继承的观点:

我们真的需要多重继承吗?

不是真的。我们可以不用
通过使用变通方法实现多重继承,就像我们可以做的那样
通过使用变通方法无需单一继承。我们甚至可以做
通过使用变通方法无需类。 C 是该论点的证明。

但是,每种现代语言都具有静态类型检查和
继承提供某种形式的多重继承。在 C++ 中,
抽象类通常充当接口,一个类可以有多个
接口。其他语言——通常被认为“不是 MI”——只是有一个
它们相当于纯抽象类的单独名称:an
界面。语言提供继承(单一继承和继承)的原因
多个)是语言支持的继承通常更优越
解决方法(例如使用转发函数到子对象或
单独分配的对象)以便于编程,用于检测
逻辑问题,为了可维护性,并且通常是为了性能。

引自 http://www2.research.att .com/~bs/bs_faq2.html#multiple

You should read Bjarne Stroustrup's point of view about multiple inheritance:

Do we really need multiple inheritance?

Not really. We can do without
multiple inheritance by using workarounds, exactly as we can do
without single inheritance by using workarounds. We can even do
without classes by using workarounds. C is a proof of that contention.

However, every modern language with static type checking and
inheritance provides some form of multiple inheritance. In C++,
abstract classes often serve as interfaces and a class can have many
interfaces. Other languages - often deemed "not MI" - simply has a
separate name for their equivalent to a pure abstract class: an
interface. The reason languages provide inheritance (both single and
multiple) is that language-supported inheritance is typically superior
to workarounds (e.g. use of forwarding functions to sub-objects or
separately allocated objects) for ease of programming, for detecting
logical problems, for maintainability, and often for performance.

quoted from http://www2.research.att.com/~bs/bs_faq2.html#multiple

满身野味 2024-12-17 18:01:20

多实现继承和多接口继承不是同一回事。

但是,如果要添加多个实现继承,GC 和其他语言的实现将会显着复杂化。

Multiple implementation inheritance and multiple interface inheritance are not the same beasts.

However, it would significantly complicate the GC and other language implementation if they were to add multiple implementation inheritance.

却一份温柔 2024-12-17 18:01:20

我认为 Java(以及许多其他 OO 语言)设计者的选择主要是受到脆弱基础问题。确实,我们不需要多重继承,但值得注意的是,我们也不需要单一继承。面向对象编程是关于实体的身份。在这方面,继承可以看作是语法糖。

I think the choice of Java (and many other OO languages) designers was mainly motivated by the fragile base problem. It's true we don't need multiple inheritance, but for that's worth to note we don't need single either. Object Oriented programming it's about identities of entities. Inheritance can be seen as syntax sugar in this regard.

夜未央樱花落 2024-12-17 18:01:20

java 中的接口概念并非旨在提供多重继承功能。

The interface concept in java is NOT meant for providing multiple inheritance feature.

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