
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!");
}
}
}以上就是C# 中的多重继承是什么?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号