回调机制

回调    

顾名思义,被调用方反过来调用调用方。

 

c/c++中用函数指针来实现:

void funtion( int x, int y)
{
   ……
}

void caller(void (*fp)(int x, int y))
{
   ……
}

void main ()
{
   
void (*fp) ( int x, int y );
   
int a = 10, b = 20;
   fun 
= function;
   caller(fun);
}


C# 用代理实现回调:

public delegate void funHandler(int a, int b);
private void Fun(int x, int y)
{
    ……
}

private void Caller(funHandler p)
{
    ……
}

funHandler fun
=new funHandler(Fun);

Caller(fun);






在上面只是说明了实现回调的基础,但是没有体现出A调用B,B又回调A这样的特征来。
要体现回调的特征,需要有A,B两个对象,A调用B的方法fb, 而fb又调用A的一个方法fa。

posted on 2008-04-21 21:08  清水无鱼  阅读(297)  评论(0)    收藏  举报

导航