C#事件与接口
C#事件與接口
using System;namespace ConsoleApplication1d {delegate void MsgDel(string s);interface IMsg{event MsgDel msgd;void Excute(string s);}class MInfo : IMsg//必須實現接口的全部成員,如事件,函數 {//不寫這句會提示 Minfo does not implement interface memeber 'IMsg.msgd' public event MsgDel msgd; //這是對接口中事件的實現!,這里所謂的【實現】比較詭異, 僅僅是重新聲明一次public void Excute(string s) //對接口中Excute的實現 {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 時空觀察者9號 閱讀(...) 評論(...) 編輯 收藏
總結
- 上一篇: C#泛型委托,匿名方法,匿名类
- 下一篇: yield(C# 参考)