using System; using System.Collections.Generic; using System.Text; //多重传送事件 namespace interfaceDemo { //多重传送事件是指一个事件同时有多个订阅者,即通过委托将多个方法添加到该事件中,当事件被引发时,同时 //触发这些委托的执行 class MutiplyEventTest { public delegate void MathOp(int i, int j);//定义委托 public event MathOp eventMath;//事件定义 static void Main(string[] args) { MutiplyEventTest et = new MutiplyEventTest();//对象实例化 //四个执行不同的方法的委托订阅同一个事件 et.eventMath += new MathOp(Add);//订阅事件,实际上是添加一个指向方法的委托,当事件引发时将通过委托调用此方法,一个时间有多个订阅者,即通过委托可以将多个方法添加到某个事件中 et.eventMath += new MathOp(Minus); et.eventMath += new MathOp(Mutiply); et.eventMath += new MathOp(Divide); Console.WriteLine("请输入两个正数:"); int m = int.Parse(Console.ReadLine()); int n = int.Parse(Console.ReadLine()); if (m > 0 & n > 0)//满足一定条件,引发事件 { et.eventMath(m, n); } Console.ReadLine(); } public static void Add(int i, int j) { Console.WriteLine("{0}+{1}={2}", i, j, i + j); } public static void Minus(int i, int j) { Console.WriteLine("{0}-{1}={2}", i, j, i - j); } public static void Mutiply(int i, int j) { Console.WriteLine("{0}*{1}={2}", i, j, i * j); } public static void Divide(int i, int j) { Console.WriteLine("{0}/{1}={2}", i, j, i / j); } } }