-- 作者:admin
-- 发布时间:11/9/2004 2:25:00 AM
-- C#快餐-13
发信人: nice (春天), 信区: DotNET 标 题: C#快餐-13 发信站: BBS 水木清华站 (Thu Jul 26 02:17:04 2001) Lesson 13. Events Let's start with a simple event algorithm. Data: John is a 3rd grade student. Event: John gets a grade (A-F) Who is affected?: Mom, dad, sister. How are they affected?: Mom and dad are happy when the grade is good (better than C) and upset otherwise. Since John and his sister just had a fight, John's sister has an opposite reaction to her parents'. Bellow is a simple program which illustrates John's family reaction to his grades. Class Test is listening to all events of type grade. Every time a new grade is received, class Test triggers an event. Class Event sends a message to a class that is subscribed to the event. If no class is subscribed, the event is not handled by the program. using System; public delegate void grade_event(string s); class Event { public event grade_event grade;//delegate grade is a member of Event public void trigger_event(string s) { if(grade!=null) grade(s); else Console.WriteLine("Event is not registered"); } } class Test { public static void catch_event(string s) { Console.WriteLine(" Grade event "+s+" is caught"); if(s=="A"||s=="B"){ Console.WriteLine("The mom and the Dad are happy with "+s); Console.WriteLine(" The sister is upset with "+s); } else if( s=="C" || s=="D" || s=="F"){ Console.WriteLine(" The mom and the dad are upset with "+s); Console.WriteLine(" The sister is happy with "+s); } else Console.WriteLine("The mom, the dad and the sister cannot understand teacher's handwriting"); } public static void Main() { Event my_event=new Event(); my_event.grade+=new grade_event(catch_event); //register grade with catch_event my_event.trigger_event("A"); my_event.trigger_event("B"); my_event.trigger_event("abrakadabra"); my_event.grade-=new grade_event(catch_event); //quit registration } } Several different classes can use the same event handler to subscribe to messages. Here is another program which illustrates this point. This program has a class Grade which sends messages about John's grades and class Health which sends messages about his health. using System; public delegate void my_event(string s); class Event { public event my_event mevent;//delegate mevent is a member of Event public void trigger_event(string s) { if(mevent!=null) mevent(s); else Console.WriteLine("Event is not registered"); } } class Grade { public static void catch_event(string s) { Console.WriteLine("Grade event "+s+" is caught"); if(s=="A"||s=="B"){ Console.WriteLine("Mam and Dad are happy with "+s); Console.WriteLine("Sister is uppset with "+s); } else if(s=="C"||s=="D"||s=="F"){ Console.WriteLine("Mam and Dad are uppset with "+s); Console.WriteLine("Sister is happy with "+s); } else Console.WriteLine("Mam, Dad and sister cannot understand teacher's h andwriting"); } public Grade() { Event test=new Event(); test.mevent+=new my_event(catch_event); //register grade with catch_ event test.trigger_event("A"); test.trigger_event("B"); test.trigger_event("abrakadabra"); test.mevent-=new my_event(catch_event); //quit registration } } class Health { public static void catch_event(string s) { Console.WriteLine("Health event "+s+" is caught"); if(s=="sick"||s=="ill"){ Console.WriteLine("Mam and Dad and the sister are worried that J ohn is"+s); } } public Health() { Event test=new Event(); test.mevent+=new my_event(catch_event); //register grade with catch_ event test.trigger_event("sick"); test.trigger_event("ill"); test.trigger_event("abrakadabra"); test.mevent-=new my_event(catch_event); //quit registration } } public class Test { public static void Main() { Health t1=new Health(); Grade t2=new Grade(); } } We will see in the next lesson how events are handled by the Graphical User Interface. -- ※ 修改:·walts 於 Jul 26 10:30:27 修改本文·[FROM: 166.111.142.118] ※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.176.234] 上一篇 返回上一页 回到目录 回到页首 下一篇
|