现在的位置: 首页 > 综合 > 正文

接口调用方法详解

2012年12月07日 ⁄ 综合 ⁄ 共 3387字 ⁄ 字号 评论关闭

接口调用方法详解

基础知识

接口定义:

    接口是一组已命名的方法签名。所以接口里可以定义方法、属性、事件,因为这些本质上都是方法。但是,接口不能定义任何的构造函数。

接口的可访问性:

    类本身的修饰符可以为public,internal等(VS2008里试过)。但是显示实现接口方法时,接口成员却不允许添加任何修饰符(VS2008里试过)。

接口的继承与调用

隐式实现接口方法:

例子:

定义个类Base,该类实现了IDisposable

// This class is derived from Object and it implements IDisposable
internal class Base : IDisposable
{
// This method is implicitly sealed and cannot be overridden
public void Dispose()
{
   Console.WriteLine("Base's Dispose");
}
}
// This class is derived from Base and it re-implements IDisposable
internal class Derived : Base, IDisposable
{
// This method cannot override Base's Dispose. 'new' is used to indicate
// that this method re-implements IDisposable's Dispose method
new public void Dispose()
{
   Console.WriteLine("Derived's Dispose");
// NOTE: The next line shows how to call a base class's implementation (if desired)
// base.Dispose();
}

}

 

结果调用显示:

public static void Main()

{

/************************* First Example *************************/

Base b = new Base();
// 用b的类型来调用Dispose:结果Base's Dispose
b.Dispose();
// 用b所指向的对象的类型来调用Dispose,
// 因为b的对象的类型IDisposable的Dispose方法是b的对象实现的
//所以结果是;Base's Dispose,和上面一样的
((IDisposable)b).Dispose();

/************************* Second Example ************************/

Derived d = new Derived();
// 用d的类型来调用Dispose,结果;Derived's Dispose
d.Dispose();
//用d所指向对象的类型来调用Dispose,结果:"Derived's Dispose"
((IDisposable)d).Dispose();

/************************* Third Example *************************/

b = new Derived();
// 用b的类型来调用Dispose,b的类型是Base。结果是:Base's Dispose
b.Dispose();
// 用b所指对象的类型调用Dispose,b所指的对象是Derived,所以结果是:Derived's Dispose
((IDisposable)b).Dispose();
}

上面接口的方法调用中,要注意两个概念:当调用一个接口方法时,到底是对象所指的类型在调用,还是类型在调用。

上述的Base类和Derived 类中的Dispose方法都隐式的实现了接口IDisposable中的Dispose方法。若对于接口IDisposable中的Dispose方法做显示实现,则可以更加清楚的看到这一点。

现在对代码做显示的实现。
显示接口方法实现

internal class Base : IDisposable

{

// This method is implicitly sealed and cannot be overridden

public void Dispose()

{

Console.WriteLine("Base's Dispose");

}

/// <summary>
/// Base显示实现IDisposable
/// </summary>
void IDisposable.Dispose()
{
Console.WriteLine("Base->IDisposable.Dispose");
}
}

internal class Derived : Base, IDisposable

{

// This method cannot override Base's Dispose. 'new' is used to indicate

// that this method re-implements IDisposable's Dispose method

new public void Dispose()

{

Console.WriteLine("Derived's Dispose");

// NOTE: The next line shows how to call a base class's implementation (if desired)

// base.Dispose();

}

/// <summary>
/// 显示实现Dispose方法
/// </summary>
void IDisposable.Dispose()
{
Console.WriteLine("Derived—IDisposable.Dispose");
}
}

同样的调用代码如下,则结果为:

public static void Main()
{
/************************* First Example *************************/
Base b = new Base();
// 用b的类型来调用Dispose,b的类型是Base,结果将调用类型的公共方法public void Dispose():结果Base's Dispose
b.Dispose();
// 用b所指的对象的类型来调用Dispose,
// 因为b的对象的类型IDisposable的Dispose方法是b的对象显示实现的,它与Base类中的公共方法

//public void Dispose()无关,它将调用接口类型IDisposable的void IDisposable.Dispose()方法

//所以结果是;Base->IDisposable.Dispose,和上面是不一样的。

((IDisposable)b).Dispose();

/************************* Second Example ************************/

Derived d = new Derived();
// 用d的类型来调用Dispose,d的类型是Derived,它将调用方法public void Dispose() 。结果是Derived's Dispose
d.Dispose();
//用d所指对象的类型来调用Dispose,d对象类型被显示转换成了IDisposable,因此它将调用

//void IDisposable.Dispose()方法,因此结果是Derived—IDisposable.Dispose

((IDisposable)d).Dispose();

/************************* Third Example *************************/

b = new Derived();
// 用b的类型来调用Dispose,b的类型是Base。结果是:Base's Dispose
b.Dispose();
// 用b所指的对象的类型调用Dispose,b所指的对象是Derived,Derived类型的对象被强制转换成接口类型IDisposable。
//它调用的是void IDisposable.Dispose()。所以结果是:"Derived—IDisposable.Dispose"
((IDisposable)b).Dispose();
}

抱歉!评论已关闭.