实现接口的类可以显式实现该接口的成员。当显式实现某成员时,不能通过类实例访问该成员,而只能通过该接口的实例访问该成员。本教程包含两个示例。第一个示例阐释如何显式实现和访问接口成员。第二个示例展示如何实现具有相同成员名的两个接口。 示例一:
interfaceIDimensions {
float Length(); float Width(); }
classBox : IDimensions {
float lengthInches; float widthInches;
public Box(float length, float width) {
lengthInches = length; widthInches = width; }
// Explicit interface member implementation: floatIDimensions.Length() {
return lengthInches; }
// Explicit interface member implementation: floatIDimensions.Width() {
return widthInches; }
publicstaticvoid Main() {
// Declare a class instance \Box myBox = newBox(30.0f, 20.0f)
IDimensions myDimensions = (IDimensions)myBox
System.Console.WriteLine(\, myDimensions.Length()); System.Console.WriteLine(\, myDimensions.Width());
}
}
实例二:
interfaceIEnglishDimensions {
float Length(); float Width(); }
// Declare the metric units interface: interfaceIMetricDimensions {
float Length(); float Width(); }
// Declare the \// IEnglishDimensions and IMetricDimensions: classBox : IEnglishDimensions, IMetricDimensions {
float lengthInches; float widthInches;
public Box(float length, float width) {
lengthInches = length; widthInches = width; }
// Explicitly implement the members of IEnglishDimensions: floatIEnglishDimensions.Length() {
return lengthInches; }
floatIEnglishDimensions.Width() {
return widthInches; }
// Explicitly implement the members of IMetricDimensions: floatIMetricDimensions.Length() {
return lengthInches * 2.54f; }
floatIMetricDimensions.Width() {
return widthInches * 2.54f; }
publicstaticvoid Main() {
// Declare a class instance \Box myBox = newBox(30.0f, 20.0f);
IEnglishDimensions eDimensions = (IEnglishDimensions)myBox;
IMetricDimensions mDimensions = (IMetricDimensions)myBox;
System.Console.WriteLine(\, eDimensions.Length()); System.Console.WriteLine(\, eDimensions.Width());
System.Console.WriteLine(\, mDimensions.Length()); System.Console.WriteLine(\, mDimensions.Width()); }
}
接口能够映射到虚方法和抽象方法上。对于虚接口,代码如下: interfaceIA { void play(); }
classFather : IA {
publicvirtualvoid play() {
Console.WriteLine(\调用基类的方法play()\); } }
classSon : Father {
publicoverridevoid play() {
Console.WriteLine(\调用派生类的方法play()\); }
}
如果为显示接口成员,则实现时不能被定义为虚成员。但可以在显示接口成员实现的过程中调用类的一个虚方法,由于可以在派生类中覆盖这个虚方法,所以同样可以达到调用不同方法的目的。
classFather : IA
{
publicvirtualvoid play() {
Console.WriteLine(\调用基类的方法play()\); }
void IA.play() {
Console.WriteLine(\调用虚方法VirtualPlay()\); VirtualPlay(); } }
接口成员映射到抽象方法上:
interfaceIA { void F(); void G(); }
abstractclassAbsClass : IA {
publicabstractvoid F(); publicabstractvoid G();
}
10.事件
C# 中的“事件”是当对象发生某些有趣的事情时,类向该类的客户提供通知的一种方法。事件最常见的用途是用于图形用户界面;通常,表示界面中的控件的类具有一些事件,当用户对控件进行某些操作(如单击某个按钮)时,将通知这些事件。
事件是类允许客户为其提供方法(事件发生时应调用这些方法)的委托的一种方法。事件发生时,将调用其客户提供给它的委托。 实例代码如下:
namespace InterfaceTest {
classEventTest { }
publicdelegatevoidChangedEventHandler(object sender, EventArgs e);//定义一个事件的委托, //object sender, EventArgs e 为事件输入参数
publicclassListWithChangedEvent:ArrayList {
publiceventChangedEventHandler Changed;//定义委托对象名,并声明是事件的委托,定义在事件的内部
protectedvirtualvoid OnChanged(EventArgs e) {
if (Changed != null)
Changed(this, e);//执行委托事件 }
publicoverrideint Add(object value) {
int i = base.Add(value);
OnChanged(EventArgs.Empty); return i; }
publicoverridevoid Clear() { base.Clear();
OnChanged(EventArgs.Empty); }
publicoverrideobjectthis[int index] { set
{ base[index] = value;
OnChanged(EventArgs.Empty); } } }
classEventListener {
privateListWithChangedEvent List;//类对象
public EventListener(ListWithChangedEvent list) {
List = list;
List.Changed += newChangedEventHandler(ListChanged);//声明委托的函数 }