//用戶去銀行取錢(發佈方),用戶收到簡訊提醒,(接收方) class Program { static void Main(string[] args) { Bank b = new Bank(); //實例化銀行 User xm = new User("... ...
//用戶去銀行取錢(發佈方),用戶收到簡訊提醒,(接收方) class Program { static void Main(string[] args) { Bank b = new Bank(); //實例化銀行 User xm = new User("110","[email protected]"); //實例化用戶 b.send += new Bank.sendEventHandle(xm.ReviceTel); //註冊事件 b.send += new Bank.sendEventHandle(xm.ReviceEmail); //註冊事件 b.issure(xm); //執行事件 Console.ReadKey(); } } public class Bank { public delegate void sendEventHandle(object o, User xx); //定義委托 public event sendEventHandle send; //基於委托的事件 public void issure(User xx) { send(this,xx); } } public class User:EventArgs{ public string tel; public string email; public User(string telephone, string email) { this.email = email; this.tel = telephone; } public void ReviceTel(object o , User x) { Console.WriteLine("收到簡訊"+ x.tel); } public void ReviceEmail(object o, User x) { Console.WriteLine("收到emile" + x.tel); } }