事件与委托的区别

事件与委托的区别在于,无论你是否定义事件是Private还是Public,类的外部都无法访问事件,只能是在类的内部进行访问。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 事件
{
    class TempatureMonitor
    {
        public delegate void StopMachineDelegate();

public event StopMachineDelegate MachineOverheating;
        public void Notity()
        {
            if(this.MachineOverheating!=null)
            {
                this.MachineOverheating();
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 事件
{
    class Program
    {
        static void Main(string[] args)
        {
            TempatureMonitor tm = new TempatureMonitor();
            WeldingMachine weldingMachine = new WeldingMachine();
            PaintingMachine paintingMachine = new PaintingMachine();
            tm.MachineOverheating += weldingMachine.FinishWelding;
            tm.MachineOverheating += paintingMachine.PaintOff;
            tm.Notity();
            Console.Read();

        }
    }
}

posted @ 2011-08-22 10:05  王永华  阅读(104)  评论(0编辑  收藏  举报