接口类型的多重继承

和类类型不同,一个接口可以扩展多个基接口。

 1 // 接口可以是多重继承
 2     interface IDrawable
 3     {
 4         void Draw();
 5     }
 6 
 7     interface IPrintable
 8     {
 9         void Print();
10         void Draw(); // 可能导致命名冲突
11     }
12 
13     // 多重接口继承
14     // IShape接口扩展了IDrawable和IPrintable
15     interface IShape : IDrawable, IPrintable
16     {
17         int GetNumberOfSides();
18     }
 1  class Rectangle : IShape
 2     {
 3         public int GetNumberOfSides()
 4         { return 4; }
 5 
 6         public void Draw()
 7         { Console.WriteLine("Drawing..."); }
 8 
 9         public void Print()
10         { Console.WriteLine("Prining..."); }
11     }
 1  // 如果愿意对每一个Draw()方法提供实现,可以使用显示接口实现解决命名冲突
 2     class Square : IShape
 3     {
 4         // 使用显示实现来处理成员命名冲突
 5         void IPrintable.Draw()
 6         {
 7             // Draw to printer ...
 8         }
 9         void IDrawable.Draw()
10         {
11             // Draw to screen ...
12         }
13         public void Print()
14         {
15             // Print ...
16         }
17         public int GetNumberOfSides()
18         { return 4; }
19     }
原文地址:https://www.cnblogs.com/ht-beyond/p/4470420.html