using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI.WebControls; namespace 回调 { class Program { static void Main(string[] args) { /*接口回调*/ Controller cl = new Controller(new CallBack()); cl.Begin(); Console.ReadKey(); } class CallBack:ICallBack { public void Run() { Console.WriteLine(DateTime.Now.ToString()); } } #region 接口回调 class Controller { //ICallBack借口定义一个Run方法,所有”回调对象“都必须实现此接口, private ICallBack CallBackObject = null;//引用回调对象 public Controller(ICallBack obj) { this.CallBackObject = obj; } public void Begin() { Console.WriteLine("敲任意键显示当前时间,按ESC键退出"); while (Console.ReadKey(true).Key != ConsoleKey.Escape) { CallBackObject.Run();//调用回调对象的方法 } } } #endregion } }