C#事件与接口
C#事件與接口
using System;namespace ConsoleApplication1d {delegate void MsgDel(string s);interface IMsg{event MsgDel msgd;void Excute(string s);}class MInfo : IMsg//必須實(shí)現(xiàn)接口的全部成員,如事件,函數(shù) {//不寫(xiě)這句會(huì)提示 Minfo does not implement interface memeber 'IMsg.msgd' public event MsgDel msgd; //這是對(duì)接口中事件的實(shí)現(xiàn)!,這里所謂的【實(shí)現(xiàn)】比較詭異, 僅僅是重新聲明一次public void Excute(string s) //對(duì)接口中Excute的實(shí)現(xiàn) {if (null != msgd)msgd(s);}}class Test{public static void Main(){IMsg msg = new MInfo();msg.msgd += MSGA;msg.msgd += MSGB;msg.Excute("hello");//output// MSGA: hello // MSGB: hello }public static void MSGA(string s){Console.WriteLine("MSGA: " + s);}public static void MSGB(string s){Console.WriteLine("MSGB: " + s);}} }?
posted on 2016-10-16 14:46 時(shí)空觀察者9號(hào) 閱讀(...) 評(píng)論(...) 編輯 收藏
總結(jié)
- 上一篇: C#泛型委托,匿名方法,匿名类
- 下一篇: U3D事件系统总结