在C#中接口可以扩展另外一个接口.比如在System.Collections命名空间中,ICollection扩展了IEnumerable,而IList又扩展了ICollection和IEnumerable.既然IList已经扩展了ICollection,而ICollection又扩展了IEnumerable,为什么在定义IList时还要再特别其指明扩展IEnumerable呢?
public interface IList:ICollection,IEnumerable 还有ArrayList 实现了IList接口,按理说ArrayList也应该同时实现ICollection,IEnumerable接口,但是定义ArrayList的类却是这样说明的: public class ArrayList:IList,ICollection,IEnumarble,ICloneable
public interface IList:ICollection,IEnumerable 还有ArrayList 实现了IList接口,按理说ArrayList也应该同时实现ICollection,IEnumerable接口,但是定义ArrayList的类却是这样说明的: public class ArrayList:IList,ICollection,IEnumarble,ICloneable
interface IInterfaceRoot{}interface InterfaceSub: InterfaceRoot{}这种接口没有InterfaceSub is IInterfaceRoot的含义,而是InterfaceSub也有IInterfaceRoot功能的意思,虽然等于true。
public interface IDrawable
{
void Draw();
}public interface IAdvancedDraw:IDrawable
{
void DrawUpsideDown();
}public class BitmapImage:AdvancedDraw
{
public void Draw(){}
public void DrawUpsideDown(){}
}
使用方法不同只是一个设计思路上的问题。
interface IControl
{
void Paint();
}interface ITextBox:IControl
{
void SetText(string text);
}interface IListBox:IControl
{
void SetItems(string[] items);
}interface IComboBox:ITextBox,IListBox
{
void SetText(string text);
}例子中接口ITextBox和IListBox都从接口IControl中继承,也就继承了接口IControl的Paint方法。接口IComboBox从接口ITextBox和IListBox中继承,因此它应该继承了接口ITextBox的SetText方法和IListBox的SetItems方法,还有IControl的Paint方法。
那么回答问题:如题实际上两种接口继承方式等价,仅仅是增加接口继承的可读性,告诉IList我也实现了IEnumerable,而不必通过继承结构告诉IList,仅此而已,即使不那样写也是可以[b]的!!!!!!!!!!![/b]