C#接口继承的问题
时间:2011-12-23
来源:互联网
public interface IList:ICollection,IEnumerable
还有ArrayList 实现了IList接口,按理说ArrayList也应该同时实现ICollection,IEnumerable接口,但是定义ArrayList的类却是这样说明的:
public class ArrayList:IList,ICollection,IEnumarble,ICloneable
作者: schinar 发布时间: 2011-12-23
作者: yanbuodiao 发布时间: 2011-12-23
interface IInterfaceRoot{}
interface InterfaceSub: InterfaceRoot{}
这种接口没有InterfaceSub is IInterfaceRoot的含义,而是InterfaceSub也有IInterfaceRoot功能的意思,虽然等于true。
作者: sunzongbao2007 发布时间: 2011-12-23
接口用于为不一定具有“是”关系的类定义特定功能。 例如,System.IEquatable(Of T) 接口可由任何类或构造实现,这些类或构造必须启用代码来确定该类型的两个对象是否等效(但是该类型定义等效性)。 IEquatable(Of T) 不表示基类和派生类之间存在的同一种“是”关系(例如 Mammal 是 Animal)。 有关更多信息,请参见 接口(C# 编程指南)。
摘自MSDN: http://msdn.microsoft.com/zh-cn/library/ms173149.aspx
作者: sunzongbao2007 发布时间: 2011-12-23
public interface IDrawable
{
void Draw();
}
public interface IAdvancedDraw:IDrawable
{
void DrawUpsideDown();
}
public class BitmapImage:AdvancedDraw
{
public void Draw(){}
public void DrawUpsideDown(){}
}
作者: schinar 发布时间: 2011-12-23
使用方法不同只是一个设计思路上的问题。
作者: sunzongbao2007 发布时间: 2011-12-23
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]
作者: ZeroKiseki 发布时间: 2011-12-23
作者: kuki4552993 发布时间: 2011-12-23
看来真如6楼说的只是增加可读性而已。
作者: qwertxp 发布时间: 2011-12-23
作者: jiezi316 发布时间: 2011-12-23
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28