///<summary> /// 事件处理函数,符合NewspaperDeliveryHandler委托定义 ///</summary> ///<param name="newspapre"></param> publicvoidReplyMessage(string newspapre) { Console.WriteLine("{0} has received the {1} newspape, thanks!", Name, newspapre); } }
运行结果:
汤乐贤 has received the issue September 24, 2019 newspape, thanks! 晋文康 has received the issue September 24, 2019 newspape, thanks! 汤乐贤 has received the issue September 25, 2019 newspape, thanks! 信鸿雪 has received the issue September 25, 2019 newspape, thanks!
publicvoidNewspaperDelivery(string newspaper) { Console.WriteLine("{0} newspaper has been delivered!", newspaper); NewspaperDeliveryEvent?.Invoke(this, new EventArgs()); }
staticvoidMain(string[] args) { //实例化报社和读者对象 NewspaperOffice newspaperOffice = new NewspaperOffice(); Readers reader1 = new Readers("汤乐贤"); Readers reader2 = new Readers("晋文康"); Readers reader3 = new Readers("信鸿雪"); //读者1和读者2订阅了报纸 newspaperOffice.NewspaperDeliveryEvent += reader1.ReplyMessage; newspaperOffice.NewspaperDeliveryEvent += reader2.ReplyMessage; //2019-09-24期报纸发行 newspaperOffice.NewspaperDelivery("issue September 24, 2019"); //读者2取消订阅报纸,读者3订阅了报纸 newspaperOffice.NewspaperDeliveryEvent -= reader2.ReplyMessage; newspaperOffice.NewspaperDeliveryEvent += reader3.ReplyMessage; //2019-09-25期报纸发行 newspaperOffice.NewspaperDelivery("issue September 25, 2019"); } }
publicclassReaders { publicstring Name { get; set; }
publicReaders(string name) { Name = name; }
///<summary> /// 事件处理函数,符合EventHandler委托定义 ///</summary> ///<param name="newspapre"></param> publicvoidReplyMessage(object s, EventArgs e) { Console.WriteLine("{0} has received the newspape, thanks!", Name); } }
运行结果:
issue September 24, 2019 newspaper has been delivered! 汤乐贤 has received the newspape, thanks! 晋文康 has received the newspape, thanks! issue September 25, 2019 newspaper has been delivered! 汤乐贤 has received the newspape, thanks! 信鸿雪 has received the newspape, thanks!
publicvoidNewspaperDelivery(string newspaper) { NewspaperDeliveryEvent?.Invoke(this, new NewspaperDeliveryEventArgs(newspaper)); }
staticvoidMain(string[] args) { //实例化报社和读者对象 NewspaperOffice newspaperOffice = new NewspaperOffice(); Readers reader1 = new Readers("汤乐贤"); Readers reader2 = new Readers("晋文康"); Readers reader3 = new Readers("信鸿雪"); //读者1和读者2订阅了报纸 newspaperOffice.NewspaperDeliveryEvent += reader1.ReplyMessage; newspaperOffice.NewspaperDeliveryEvent += reader2.ReplyMessage; //2019-09-24期报纸发行 newspaperOffice.NewspaperDelivery("issue September 24, 2019"); //读者2取消订阅报纸,读者3订阅了报纸 newspaperOffice.NewspaperDeliveryEvent -= reader2.ReplyMessage; newspaperOffice.NewspaperDeliveryEvent += reader3.ReplyMessage; //2019-09-25期报纸发行 newspaperOffice.NewspaperDelivery("issue September 25, 2019"); } }
publicclassReaders { publicstring Name { get; set; }
publicReaders(string name) { Name = name; }
///<summary> /// 事件处理函数,符合NewspaperHandler委托定义 ///</summary> ///<param name="newspapre"></param> publicvoidReplyMessage(object s, NewspaperDeliveryEventArgs e) { Console.WriteLine("{0} has received the {1} newspape, thanks!", Name, e.Newspaper); } }
运行结果:
汤乐贤 has received the issue September 24, 2019 newspape, thanks! 晋文康 has received the issue September 24, 2019 newspape, thanks! 汤乐贤 has received the issue September 25, 2019 newspape, thanks! 信鸿雪 has received the issue September 25, 2019 newspape, thanks!