昨天看了委托与事件,今天做了个小例子来和大家分享,希望对大家有帮助
程序代码:
using System; using System.Collections.Generic; using System.Text; namespace TestLibrary { public class Car { private string name; public string Name { get { return name; } set { if (value == null) { name = "桑塔纳"; } else { name = value; } } } public void MoveCar(object sender , DriverEventArgs e) { Driver d = (Driver)sender; if (e.IsMove) { Console.WriteLine("{0}开动了{1}!",d.Name,name); } else { Console.WriteLine("{0}关掉了{1}引擎!", d.Name, name); } } public Car() { this.Name = null; } public Car(string name) { this.Name = name; } } } using System; using System.Collections.Generic; using System.Text; namespace TestLibrary { public delegate void DriverCarHandler(object sender,DriverEventArgs e); public class DriverEventArgs : EventArgs { private bool isMove;//是否开动汽车 public bool IsMove { get { return isMove; } set { isMove = value; } } public DriverEventArgs(bool isMove) { this.IsMove = isMove; } } public class Driver { public event DriverCarHandler DriverCarEventHandler; private string name; public string Name { get { return name; } set { name = value; } } public void DriverCar(Object sender , LightEventArgs e) { Light l = (Light)sender; Console.WriteLine("{0}看到交通灯信息:{1}。", name, e.Message); if (l.IsLight) { Console.WriteLine("{0}兴奋的大喊道:哈,是绿灯,太走运了!", name); } else { Console.WriteLine("{0}沮丧的叹气:哎,又吃红灯了,今天真倒霉!", name); } if (DriverCarEventHandler != null) { DriverCarEventHandler(this, new DriverEventArgs(l.IsLight)); } } private Driver() { } public Driver(string name) { this.Name = name; } } } using System; using System.Collections.Generic; using System.Text; namespace TestLibrary { public delegate void ChangeIsLightHandler(object sender, LightEventArgs e); public class LightEventArgs : EventArgs { private string message; public string Message { get { return message; } set { message = value; } } public LightEventArgs(string message) { this.Message = message; } } public class Light { public event ChangeIsLightHandler ChangeIsLightEventHandler; private bool isLight;//True = 绿,False = 红 public bool IsLight { get { return isLight; } set { isLight = value; } } public void ChangeIsLight(object sender, EventArgs e)//灯状态切换的方法 { string message; if (isLight) { IsLight = false; message = "现在是红灯,请停车!"; } else { IsLight = true; message = "现在是绿灯,请有序通过!"; } if (ChangeIsLightEventHandler != null) { ChangeIsLightEventHandler(this, new LightEventArgs(message)); } Console.WriteLine(); } public Light() { } public Light(bool isLight) { this.IsLight = isLight; } } } using System; using System.Collections.Generic; using System.Text; using TestLibrary; using System.Timers; namespace TestConsole { class Program { static void Main(string[] args) { Timer timer = new Timer(5000); Light l1 = new Light(); Light l2 = new Light(true); Driver d1 = new Driver("林志玲"); Car c1 = new Car("法拉利"); Driver d2 = new Driver("程序员"); Car c2 = new Car(); l1.ChangeIsLightEventHandler += d1.DriverCar; l2.ChangeIsLightEventHandler += d2.DriverCar; d1.DriverCarEventHandler += c1.MoveCar; d2.DriverCarEventHandler += c2.MoveCar; timer.Elapsed += new ElapsedEventHandler(l1.ChangeIsLight); timer.Elapsed += new ElapsedEventHandler(l2.ChangeIsLight); timer.Start(); Console.ReadKey(); } } }