【问题标题】:Multiple inheritance in C#C#中的多重继承
【发布时间】:2026-01-25 14:15:02
【问题描述】:

由于我是C# 开发人员,我知道我们可以通过使用Interface 来实现multiple inheritance

任何人都可以提供链接或代码,了解如何使用C# 实现multiple inheritance

我想要关于如何在C# 中使用Interface 实现多重继承的代码。

提前致谢。

【问题讨论】:

  • C#可以实现多个接口,但只能继承一个类。
  • 什么时候实现多个接口就等于多个'继承'了?实现和继承是两个完全不同的东西。
  • @Hasan:不是'完全',从接口继承满足替换原则。
  • @Henk 是的,但存在语义差异。接口代表行为,类代表实体。
  • @Hasan / @Henk:Meyer 定义了十六种(多)继承(p824+),但是这里讨论的接口和类之间的区别仅定义了两种:多实现继承和多接口继承(或行为继承,或 Meyer 所称的子类型继承)。 C#只支持单实现继承和多接口继承。重点是:接口继承不等于,而是多重继承的一种形式。

标签: c# oop inheritance multiple-inheritance


【解决方案1】:

这不是严格意义上的多重继承——但你的 C# 类可以实现多个接口,没错:

public class MyClass : BaseClass, IInterface1, IInterface2, IInterface3
{
   // implement methods and properties for IInterface1
   ..

   // implement methods and properties for IInterface2
   ..

   // implement methods and properties for IInterface3
   ..
}

您需要为您计划实现的所有接口中定义的所有方法(和属性)提供实现。

我不太清楚你从你的问题中寻找什么......你能澄清一下吗?你想做什么?你在哪里遇到麻烦??

【讨论】:

  • Marc,我想知道我们如何才能在使用接口的多重继承中获得确切的行为,就像我们继承单个类一样。还想知道我们可以使用接口将当前对象转换为另一个类吗? (可能这叫MH)
  • 无法通过实现接口获得与真正的多重继承完全相同的行为。接口只定义一个功能接口——你不能从多个实现继承。这是一个近似值 - 不是一个精确的替代值。
  • “严格的多重继承”不存在,但是一些共识似乎是“严格”意味着多重实现继承。事实上,多接口继承只是多继承的另一种形式,其中存在多种形式(Meyer 在 OOSC 中描述了 16 种这样的形式 p824+)。
【解决方案2】:

这是一个很好的例子。

http://blog.vuscode.com/malovicn/archive/2006/10/20/How-to-do-multiple-inheritance-in-C_2300_-2D00-Implementation-over-delegation-_2800_IOD_2900_.aspx

快速代码预览:

interface ICustomerCollection
{
      void Add(string customerName);
      void Delete(string customerName);
}

class CustomerCollection : ICustomerCollection
{
      public void Add(string customerName)
      {
            /*Customer collection add method specific code*/
      }
      public void Delete(string customerName)
      {
            /*Customer collection delete method specific code*/
      }
}

class MyUserControl: UserControl, ICustomerCollection
{
      CustomerCollection _customerCollection=new CustomerCollection();

      public void Add(string customerName)
      {
            _customerCollection.Add(customerName);
      }
      public void Delete(string customerName)
      {
            _customerCollection.Add(customerName);
      }
}

【讨论】:

    【解决方案3】:

    实现多个接口不是替代多重继承。接口用于指定类将遵守的合同。更像是抽象类。

    如果你想实现多继承的效果,实现Composite Pattern可以帮到你。

    【讨论】:

      【解决方案4】:
      class MyClass : IFirstInterface, ISecondInterface, IThirdInterface
      {
        // implement the methods of the interfaces
      }
      

      【讨论】:

        【解决方案5】:

        那么有这个可以从多个接口继承:

        class EmptyClass : IDoSomething, IDoSomethingElse
        {
        }
        interface IDoSomething
        {
        }
        interface IDoSomethingElse
        {
        }
        
        static class InterfaceExtensions
        {
            public static int DoSomething(this IDoSomething tThis)
            {
                return 8;
            }
            public static int DoSomethingElse(this IDoSomethingElse tThis)
            {
                return 4;
            }
        }
        

        在接口上使用扩展方法,您可以获得更像多重继承的东西,而不仅仅是附加接口。由于方法不是接口定义的一部分,因此您也不必在类中实现它们,除非您愿意。 (并没有真正覆盖它们,您需要一个 EmptyClass 类型的引用来调用它们,因为更具体或确切的类型名称会胜过继承的类型。

        【讨论】: