您的位置:首页 > 编程语言 > C#

C# 复习(1) 委托与事件

2013-09-12 21:38 274 查看
委托定义顺序

1. 声明一个委托

2.定义一个委托变量

3. 委托变量的初始化或者给委托变量绑定一个方法

4.调用委托

事件:事件是对委托的封装。

事件只能在创建事件的类的内部调用。

public class Controller
{

public delegate void stopMachineryDelegate();
public event stopMachineryDelegate StopMachine;
public void ShutDown()
{
for (int i = 0; i < 100; i++)
{
if (i > 50)
{
if (this.StopMachine != null)
{
this.StopMachine();
}
}
}

}
}


static void Main(string[] args)
{
Controller controller=new Controller();

FoldingMachine folder = new FoldingMachine();
PaintingMachine painter = new PaintingMachine();
WeldingMachine welder = new WeldingMachine();

controller.StopMachine += folder.StopFolding;
controller.StopMachine += painter.PaintOff;
controller.StopMachine += welder.FinishWelding;

controller.ShutDown();

Console.ReadKey();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: