C# 接口的定义和使用

在进行接口映射时,还要注意下面两点:

  1、在决定由类中的哪个成员来实现接口成员时,类中显式说明的接口成员比其它成员优先实现。

  2、使用Private、protected和static修饰符的成员不能参与实现接口映射。例如:

 

 

代码
interface ICloneable {
 
object Clone( ) ;
}
class C: ICloneable {
 
object ICloneable.Clone( ) {…}
 
public object Clone( ) {…}
}

  
/*例子中成员ICloneable.Clone 称为接口ICloneable 的成员Clone 的实现者,因为它是显式说明的接口成员,比其它成员有着更高的优先权。

  如果一个类实现了两个或两个以上名字、类型和参数类型都相同的接口,那么类中的一个成员就可能实现所有这些接口成员:
*/

interface IControl {
 
void Paint( ) ;
}
interface IForm {
 
void Paint( ) ;
}
class Page: IControl, IForm {
 
public void Paint( ) {…}
}
  
//这里,接口IControl和IForm的方法Paint都映射到了类Page中的Paint方法。当然也可以分别用显式的接口成员分别实现这两个方法:
interface IControl {
 
void Paint( ) ;
}
interface IForm {
 
void Paint( ) ;
}
class Page: IControl, IForm {
 
public void IControl.Paint( ) {
 
//具体的接口实现代码
}
public void IForm.Paint( ) {
 
//具体的接口实现代码
}
}

 

posted on 2010-11-09 17:16  Jan.David  阅读(612)  评论(0编辑  收藏  举报

导航