接口编程全解析:从多重继承到克隆实现
1. 接口类型的多重继承
与类类型不同,接口可以扩展多个基接口,这为设计强大而灵活的抽象提供了可能。我们可以创建一个名为 MIInterfaceHierarchy 的新控制台应用程序项目,以下是一组模拟各种渲染和形状抽象的接口:
// Multiple inheritance for interface types is a-okay.
interface IDrawable
{
void Draw();
}
interface IPrintable
{
void Print();
void Draw(); // <-- Note possible name clash here!
}
// Multiple interface inheritance. OK!
interface IShape : IDrawable, IPrintable
{
int GetNumberOfSides();
}
此时,如果有一个类支持 IShape 接口,需要实现的方法数量取决于具体需求。如果想为 Draw() 方法提供简单实现,只需提供三个成员,如以下 Rectangle 类型所示:
class Rectangle : IShape
{
public int GetNumberOfSides()
{ return 4;
超级会员免费看
订阅专栏 解锁全文
715

被折叠的 条评论
为什么被折叠?



