C#中可以使用接口实现多重继承吗?

发布于 2024-09-29 01:44:15 字数 206 浏览 0 评论 0原文

我经常在描述抽象类和接口的文章中遇到,C#不支持多重继承,但可以使用接口来实现。我不同意这一点,原因如下:

  1. 我们总是从任何类继承状态和行为。
  2. 接口不定义状态或行为。
  3. 我们不能从接口继承任何东西,但总是要实现它。

所以底线是C#不支持多重继承,接口不能帮助我们实现多重继承(事实上我们永远不能从接口继承)。

I often encounter in articles which describe abstract class and interface, that C# does not support multiple inheritance but can be achieved using interfaces. I don't agree to that for the following reasons

  1. We always inherit the state and behavior from any class.
  2. Interface does not define the state or behavior.
  3. We cannot inherit anything from an interface but always implement it.

So bottom line is that C# does not support multiple inheritance and interfaces cannot help us achieve multiple inheritance (in fact we can never inherit from an interface).

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

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

发布评论

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

评论(2

不一样的天空 2024-10-06 01:44:15

不,您不能在 C# 中实现多重继承。

但是,您可以通过接口获得多重继承的一些好处,即可以向类添加契约以使其适合系统的其他部分的部分,即替换原则。

但是不,您无法获得从多个基类继承行为的部分。

No, you can not implement multiple inheritance in C#, period.

You can, however, get some of the benefits of multiple inheritance through interfaces, namely the part where you can add contracts to a class to make it fit other parts of the system, the substitution principle.

But no, you can not get the part where you inherit behavior from multiple base classes.

秋风の叶未落 2024-10-06 01:44:15

事实上我们永远不能从接口继承

Disputable 继承。从界面“继承”是一种查看界面的方式。它确实满足替代原则。

但我同意“实现”接口是更常见的表达/视图。

in fact we can never inherit from an interface

Disputable. "Inheriting" from an interface is a way of viewing it. It does satisfy the substitution principle.

But I agree that "Implementing" an interface is a more common expression/view.

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