新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → C#快餐-13 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2342 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: C#快餐-13 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18406
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客楼主
    发贴心情 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]
    上一篇
    返回上一页
    回到目录
    回到页首
    下一篇


       收藏   分享  
    顶(0)
      




    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/9 2:25:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/12/27 17:59:08

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    78.125ms