C#中的接口
1.都是“虚的”不能被实例化,这也是接口中为什么不能包含字段--成员变量的原因2.正因为接口是虚的,所以接口内的索引,属性,时间等只能有声明,而不能在接口内实现,具体如何实现是派生接口或者派生类的事.
3.都具有模板的性质,如果一个接口或者类从某一个接口继承,它将自动具有被集成者的特征(包括索引,属性,函数,实践等).
4.接口支持多重继承,而C#中,类之支持单一继承,接口实际表示的是一种承载能力,
下面是接口的一个简单定义:
interface
SampInterface1
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
string
this[ int index]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get;
set
}
;
event EventHandler Event;
void Find( int value );
//注意此处没有
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
string Po
int
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get;
set
}
;
}
上面的接口定义了一个索引
this,一个实践Event,一个方法Find和一个属性Point.
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public interface SampIneterface:SampIneterface1
{
pravite int a=1;
void find (int value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
a+=value;
}
event EventHandler Event;
protected void onEvent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(Event=null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{return Event(this.System.EventAgrs.Empty;)}
}
![](/Images/OutliningIndicators/InBlock.gif)
}