C#委託和事件

來源:互聯網
上載者:User

標籤:c#   委託   

C#委託,我看了很長時間的資料和文章,
以前對委託的理解就是委託其實就是將一個方法作為參數傳遞給第一個方法。
現在對委託的認識則變了一些的,委託可以實現:
                    1.在兩個不能直接調用的方法之間做為橋樑
                    2.當不知具體實現什麼的時候適用委託(如事件)
使用一個委託時,需要兩個步驟:
1.定義一個委託,就像是定義一個類一樣;
2.執行個體化一個或多個該委託。
3.為委託註冊方法。
在定義委託的時候,往往會在某類的外部建立,並且是public(相當於全域變數,因為聲明委託的目的就是為了把它暴露在類的用戶端進行方法的註冊,如果是private,用戶端對它根本不可見,就無用了)
下面用執行個體來講一下委託的使用
第一個執行個體:
情境是這樣的:1.我叫朋友和朋友的女朋友出來玩  2.我叫朋友出來玩,再帶上她女朋友
這個情境(我們看第二種)裡我沒有許可權叫朋友的女朋友出來玩!!!(你懂的),但是我們可以委託朋友讓他叫他女朋友出來。
代碼如下
<span style="white-space:pre"></span>#region myOwnExmple        public delegate void delegate_Exmple();        public static void TestExmple() {            Me me = new Me();            me.Name = "richard【我的名字】";            Friend f = new Friend();            f.Name = "neil【我朋友的名字】";            f.GFName = "leo【我朋友女朋友的名字】";            //Me沒有許可權叫f的GF            me.delegate2 = f.callGF;//註冊方法            me.callFriend(f);        }        public class Me        {            public delegate_Exmple delegate2;            public string Name { get; set; }            public void callFriend(Friend f)            {                Console.WriteLine(Name + "自己肯定先來了");                Console.WriteLine(f.Name + "已經來了");                if (delegate2 != null)                {                    delegate2();                }            }        }        public class Friend        {            public string Name { get; set; }            public string GFName { get; set; }            public void callGF()            {                Console.WriteLine(GFName + "已經來了");            }        }        #endregion


第二個執行個體:
觀察者模式(分兩種:1.只用委託(在每個region內,登出的部分)2.將委託和事件結合起來)
<span style="white-space:pre"></span>#region 觀察者模式        public delegate void delegate_Observe(int temperature);        public static void TestObserve() {            Heater heater = new Heater();            #region 委託   如果使用委託,則必須將委託寫在外面(類似於全域變數)            //heater.delegate1 = new Alarm().MakeAlert;            //heater.delegate1 += new Monitor().ShowMessage;            //heater.Boil();            #endregion            #region 事件            heater.eventTellOther += (new Alarm()).MakeAlert;//匿名委託            heater.eventTellOther += new Monitor().ShowMessage;            heater.Boil();//先註冊方法再執行            #endregion        }        //觀察者模式 熱水器被委託(所以要在熱水器方法內添加委託)去告訴警示器和顯示器去做事情        public class Heater        {            public int Temperature { get; set; }            #region 使用委託,但是需要全域變數            //public delegate_Observe delegate1;            #endregion            #region 使用事件            public delegate void TellOther(int Temperature);            public event TellOther eventTellOther;            #endregion            public void Boil()            {                for (int i = 0; i <= 100; i++)                {                    Temperature = i;                    if (Temperature % 10 == 0)                    {                        #region 使用委託,但是需要全域變數                        //if (delegate1 != null)  //如果有方法註冊委託變數                         //聯想到其實這個委託定義就是一個全域變數,只有任何地方將其執行個體化,並且為其註冊方法                        //{                        //    delegate1(Temperature);  //通過委託調用方法                        //}                        #endregion                        #region 使用事件                        if (eventTellOther != null)                        {                            eventTellOther(Temperature);                        }                        #endregion                    }                }            }        }        public class Alarm        {            public void MakeAlert(int temperature)            {                Console.WriteLine("水溫已經{0}度,發出嗚嗚嗚的鳴叫聲~", temperature);            }        }        public class Monitor        {            public void ShowMessage(int temperature)            {                Console.WriteLine("水溫已經{0}度,顯示出資訊", temperature);            }        }        #endregion

從第一個例子我們可以稍微感覺到委託的好處。但是無奈接觸後不多,不能給出更好的例子,望各位大牛指點。

C#委託和事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.