C# 中的多重继承是什么?
csharpprogrammingserver side programming更新于 2025/10/14 2:52:17
C# 不支持多重继承,但可以使用接口实现。
以下是使用接口实现继承的示例。创建两个接口 −
public interface BaseOne {
void display();
}
public interface BaseTwo {
void display();
}
现在像设置派生类一样设置接口。
public class ChildOne : BaseOne, BaseTwo {
public void display() {
Console.WriteLine("Child Class!");
}
}
我们将调用子类函数,如以下代码所示,在 C# 中实现多重继承 −
示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo {
class Program {
static void Main(string[] args) {
ChildOne c = new ChildOne();
c.display();
Console.ReadKey();
}
}
public interface BaseOne {
void display();
}
public interface BaseTwo {
void display();
}
public class ChildOne : BaseOne, BaseTwo {
public void display() {
Console.WriteLine("Child Class!");
}
}
}

