C#事件機制-複習

來源:互聯網
上載者:User
事件機制

    class Person    {        public string name;        public int age;        public string sex;        public float money;        public Person()        {            this.name = "張三";            this.age = 18;            this.sex = "男";            this.money = 1000;        }        public Person(string name, int age, string sex,float money)        {            this.name = name;            this.age = age;            this.sex = sex;            this.money = money;        }    }    class Custom : Person    {       public delegate void BuyEventHandler(object sender);       public event BuyEventHandler BuyEvent;        public void Buy()       {           if(this.money==0)               BuyEvent(this);       }    }    class Seller:Person    {               public Seller(Custom c)        {            c.BuyEvent += new Custom.BuyEventHandler(this.Warning);        }        public void Warning(object sender)        {                       Console.WriteLine("沒錢了請儲值!");        }    }    

 

View Code

using System;using System.Collections.Generic;using System.Linq;using System.Text;// 定義事件包含資料 public class MyEventArgs : EventArgs{    private string StrText;    public MyEventArgs(string StrText)    {        this.StrText = StrText;    }    public string GetStrText    {        get        {            return StrText;        }    }}// 發布事件的類 class EventSource{    MyEventArgs EvArgs = new MyEventArgs("觸發事件");    // 定義委託     public delegate void EventHandler(object sender, MyEventArgs e);    // 定義事件     public event EventHandler TextOut;    // 啟用事件的方法     public void TriggerEvent()    {        if (TextOut == null)            TextOut(this, EvArgs);    }} // 訂閱事件的類 class TestApp{    public static void Main()    {        EventSource evsrc = new EventSource();        // 訂閱事件         evsrc.TextOut += new EventSource.EventHandler(CatchEvent);        // 觸發事件         evsrc.TriggerEvent();        Console.WriteLine("------");        // 取消訂閱事件         evsrc.TextOut -= new EventSource.EventHandler(CatchEvent);        // 觸發事件         evsrc.TriggerEvent();        Console.WriteLine("------");  // 事件訂閱已取消,什麼也不執行         TestApp theApp = new TestApp();        evsrc.TextOut += new EventSource.EventHandler(theApp.InstanceCatch);        evsrc.TriggerEvent();        Console.WriteLine("------");    }    // 處理事件的靜態方法     public static void CatchEvent(object from, MyEventArgs e)    {        Console.WriteLine("CatchEvent:{0}", e.GetStrText);    }    // 處理事件的方法     public void InstanceCatch(object from, MyEventArgs e)    {        Console.WriteLine("InstanceCatch:{0},e.GetStrText");    }}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.