首页 > Windows开发 > 详细

C#事件与接口

时间:2016-10-16 16:07:07      阅读:154      评论:0      收藏:0      [点我收藏+]
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);
        }
    }
}

 

C#事件与接口

原文:http://www.cnblogs.com/timeObjserver/p/5966629.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!