interface IShape {
void Draw ( ) ;
}
如果你从两个或者两个以上的接口派生,父接口的名字列表用逗号分隔,如下面的代码所示:
interface INewInterface: IParent1, IParent2 { }
然而,与Java不同,C#中的接口不能包含域(Field)。另外还要注意,在C#中,接口内的所有方法默认都是公用方法。在Java中,方法定义可以带有public修饰符(即使这并非必要),但在C#中,显式为接口的方法指定public修饰符是非法的。例如,下面的C#接口将产生一个编译错误。
interface IShape { public void Draw( ) ; }
下面的例子定义了一个名为IControl 的接口,接口中包含一个成员方法Paint:
interface IControl {
void Paint( ) ;
}
在下例中,接口 IInterface从两个基接口 IBase1 和 IBase2 继承:
interface IInterface: IBase1, IBase2 {
void Method1( ) ;
void Method2( ) ;
}
接口可由类实现。实现的接口的标识符出现在类的基列表中。例如:
class Class: Iface, Iface {
// class 成员。
// http://www.easck.com/roucheng/
}
类的基列表同时包含基类和接口时,列表中首先出现的是基类。例如:
class ClassA: BaseClass, Iface1, Iface2 {
// class成员。
}
以下的代码段定义接口IFace,它只有一个方法:
interface IFace {
void ShowMyFace( ) ;
}
不能从这个定义实例化一个对象,但可以从它派生一个类。因此,该类必须实现ShowMyFace抽象方法:
class CFace:IFace
{
public void ShowMyFace( ) {
Console.WriteLine(" implementation " ) ;
}
}
基接口
一个接口可以从零或多个接口继承,那些被称为这个接口的显式基接口。当一个接口有比零多的显式基接口时,那么在接口的定义中的形式为,接口标识符后面跟着由一个冒号":"和一个用逗号","分开的基接口标识符列表。
接口基:
:接口类型列表说明:










