回调函数
Javascript
function person(text,callback) {
alert(text);
callback(text).apply();
}
function person_callback(text)
{
alert('person: ' +text + '_callback');
}
function computer_callback(text)
{
alert('computer: ' + text + '_callback');
}
person('hello',computer_callback)
========================================================
C#
using System;
using System.Collections.Generic;
using System.Text;
namespace App
{
public delegate void CallbackHandler(string text);
public class Person
{
public void Say(string text, CallbackHandler handler)
{
Console.WriteLine(text);
handler.Invoke(text + "_callback");
}
public void Say_Callback(string text)
{
Console.WriteLine("person: " + text);
}
}
public class Computer
{
public void Say_Callback(string text)
{
Console.WriteLine("computer: " + text);
}
}
class Program
{
static void Main(string[] args)
{
Person p = new Person();
CallbackHandler personHandler = new CallbackHandler((new Person()).Say_Callback);
CallbackHandler computerHandler = new CallbackHandler((new Computer()).Say_Callback);
p.Say("hello", personHandler);
p.Say("hello", computerHandler);
}
}
}