c#委托与事件2

首先是一个关机器的一般方法:

c#委托与事件2

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DelegateDemo
{
class Program
{
static void Main(string[] args)
{
FoldingMachine folder = new FoldingMachine();
WeldingMachine welder = new WeldingMachine();
PaintingMachine painter = new PaintingMachine(); Controller controller = new Controller();
controller.Folder = folder;
controller.Welder = welder;
controller.Painter = painter;
controller.ShutDown();
Console.ReadKey();
}
}
}

Program.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DelegateDemo
{
class Controller
{
private FoldingMachine folder;
private WeldingMachine welder;
private PaintingMachine painter;
public void ShutDown()
{
folder.FinishFolding();
welder.FinishWelding();
painter.PaintOff();
}
public FoldingMachine Folder
{
set { this.folder = value; }
} public WeldingMachine Welder
{
set { this.welder = value; }
} public PaintingMachine Painter
{
set { this.painter = value; }
} }
}

Controller.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DelegateDemo
{
class FoldingMachine
{
public void StopFolding(int ShutDownTime)
{
Console.WriteLine("stop FoldingMachine");
} public void FinishFolding()
{
StopFolding();
}
}
}

FoldingMachine.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DelegateDemo
{
class PaintingMachine
{
public void PaintOff()
{
Console.WriteLine("stop PaintingMachine");
}
}
}

PaintingMachine.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DelegateDemo
{
class WeldingMachine
{
public void FinishWelding()
{
Console.WriteLine("stop WeldingMachine.");
}
}
}

WeldingMachine.cs


c#委托与事件2

c#委托与事件2

c#委托与事件2

c#委托与事件2

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DelegateDemo
{
class Program
{
static void Main(string[] args)
{
FoldingMachine folder = new FoldingMachine();
WeldingMachine welder = new WeldingMachine();
PaintingMachine painter = new PaintingMachine(); Controller controller = new Controller();
controller.Folder = folder;
controller.Welder = welder;
controller.Painter = painter;
controller.ShutDown();
controller.StopManchinery();
Console.ReadKey();
}
}
}

Program.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DelegateDemo
{
class Controller
{
private FoldingMachine folder;
private WeldingMachine welder;
private PaintingMachine painter;
public delegate void stopManchineryDelegate();
stopManchineryDelegate stopManchinery; public Controller() { //
}
public stopManchineryDelegate StopManchinery
{
get { return stopManchinery; }
set { stopManchinery += value; }
}
public void SetStopManchinery()
{
//stopManchinery = new stopManchineryDelegate(folder.StopFolding);
stopManchinery += folder.StopFolding;
stopManchinery += welder.FinishWelding;
stopManchinery += painter.PaintOff; }
public void ShutDown()
{
//folder.FinishFolding();
//welder.FinishWelding();
//painter.PaintOff();
SetStopManchinery();
}
public FoldingMachine Folder
{
set { this.folder = value; }
} public WeldingMachine Welder
{
set { this.welder = value; }
} public PaintingMachine Painter
{
set { this.painter = value; }
} }
}

Controller.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DelegateDemo
{
class FoldingMachine
{
public void StopFolding()
{
Console.WriteLine("stop FoldingMachine");
} public void FinishFolding()
{
StopFolding();
}
}
}

FoldingMachine

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DelegateDemo
{
class PaintingMachine
{
public void PaintOff()
{
Console.WriteLine("stop PaintingMachine");
}
}
}

PaintingMachine

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DelegateDemo
{
class WeldingMachine
{
public void FinishWelding()
{
Console.WriteLine("stop WeldingMachine.");
}
}
}

WeldingMachine.cs

注意委托(其实就是指针,指向函数的指针,也就是函数指针)的参数要与所指向的函数的参数相同,体现到代码中就是FoldingMachine.cs中的修改。另外一个类中的字段即使是public也不能在另一个类中直接使用,所以代码中使用了方法(controller.StopManchinery();)来使用另一个类中的委托。


lamada表达式:

c#委托与事件2

c#委托与事件2

c#委托与事件2


事件:

c#委托与事件2

事件的调用只能在定义事件的类的内部调用,无论是用public还是private修饰事件。委托违反了封装的原则,事件封装委托,保证了封装。


参考:http://blog.csdn.net/jamestaosh/article/details/4372172

要创建一个事件驱动的程序需要下面的步骤:

1.         声明关于事件的委托;

2.         声明事件;

3.         编写触发事件的函数;

4.         创建事件处理程序;

5.         注册事件处理程序;

6.         在适当的条件下触发事件。 

现在我们来编写一个自定义事件的程序。主人养了一条忠实的看门狗,晚上主人睡觉的时候,狗负责看守房子。一旦有小偷进来,狗就发出一个Alarm事件,主人接到Alarm事件后就会采取相应的行动。假设小偷于2009年元旦午夜时分到达。

//事件发送者

class Dog
{
//1.声明关于事件的委托
public delegate void AlarmEventHandler(object sender, EventArgs e); //2.声明事件
public event AlarmEventHandler Alarm; //3.编写引发事件的函数
public void OnAlarm()
{
if (this.Alarm != null)
{
Console.WriteLine("/n狗报警: 有小偷进来了,汪汪~~~~~~~");
this.Alarm(this, new EventArgs()); //发出警报
}
}
} //事件接收者
class Host
{
//4.编写事件处理程序
void HostHandleAlarm(object sender, EventArgs e)
{
Console.WriteLine("主 人: 抓住了小偷!");
} //5.注册事件处理程序
public Host(Dog dog)
{
dog.Alarm += new Dog.AlarmEventHandler(HostHandleAlarm);
}
} //6.现在来触发事件
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
Host host = new Host(dog); //当前时间,从2008年12月31日23:59:50开始计时
DateTime now = new DateTime(2008, 12, 31, 23, 59, 50);
DateTime midnight = new DateTime(2009, 1, 1, 0, 0, 0); //等待午夜的到来
Console.WriteLine("时间一秒一秒地流逝... ");
while (now < midnight)
{
Console.WriteLine("当前时间: " + now);
System.Threading.Thread.Sleep(1000); //程序暂停一秒
now = now.AddSeconds(1); //时间增加一秒
} //午夜零点小偷到达,看门狗引发Alarm事件
Console.WriteLine("/n月黑风高的午夜: " + now);
Console.WriteLine("小偷悄悄地摸进了主人的屋内... ");
dog.OnAlarm();
}

http://blog.csdn.net/Wiiix/article/details/51463977

下面再看一个例子:

在我们创建一个事件之前,我们需要一个委托,而一般标准的委托声明如下:

public delegate void EventHandler(object sender, System.EventArgs e);

第一个形参object sender定义了对象来源,第二个形参放的是继承自System.EventArgs的类,一般上这个类包含了事件的详细信息。

例子:

class ButtonEventArgs:EventArgs

{

public string time;

}

在这里我们不需要传递什么事件信息,所以我们用基类EventArgs就好。

  public delegate void EventHandler(object sender, System.EventArgs e);

     class Publisher
{
public event EventHandler Added; //定义发生事件 protected virtual void OnAdded(System.EventArgs e) //当事件发生中触发方法
{
if(Added!=null)
{
Added(this, e);
}
}
public void Add(object value) //触发事件的方法
{ OnAdded(System.EventArgs.Empty);
}
} class Subscriber
{
void AddedEventHandler(object sender,System.EventArgs e)
{
System.Console.WriteLine("AddEvent occured");
} static void Main()
{
Subscriber s = new Subscriber();
Publisher p = new Publisher();
p.Added += s.AddedEventHandler;
p.Add();
}
}

事件的使用步骤如下:

存放事件的类

1.定义事件

2.触发事件的方法(protected)

3.间接触发事件的方法(public)

触发事件的类

1.定义方法

2.注册方法

3.触发方法

 

http://www.cnblogs.com/yinqixin/p/5067033.html


 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 自定义事件一
{
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
Host host = new Host(dog );
DateTime now = new DateTime(, , , , , );
DateTime midnight = new DateTime(, , , , , );
while (now < midnight)
{
Console.WriteLine("当前时间" + now);
System.Threading.Thread.Sleep();
now = now.AddSeconds();
}
Console.WriteLine("\n月黑风高的午夜:" + now);
Console.WriteLine("小偷进了主人的屋内...........");
dog.OnAlarm();
Console.ReadKey();
}
}
class Dog
{
//第一:声明关于事件的委托
public delegate void AlarmEventHandler(object sender, EventArgs e);
//第二:声明事件
public event AlarmEventHandler Alarm;//前面的public没用,这个Alarm事件仍然是private的,可以在类外绑定方法,不能运行这个事件
//第三:编写引发shijian的函数
public void OnAlarm()
{
if (this.Alarm != null)
{
Console.WriteLine("\n狗报警,有小偷进来了,汪汪汪~~~");
this.Alarm(this, new EventArgs());//运行这个事件;this 当前对象,this.Alarm是当前对象多引用的事件
}
}
}
class Host
{
//第四:编写事件的处理程序
void HostHandeAlarm(object sender, EventArgs e)
{
Console.WriteLine("主人:抓住了小偷!~");
}
//第五:注册事件的处理程序
public Host(Dog dog)
{
dog.Alarm += new Dog.AlarmEventHandler(HostHandeAlarm);
}
} }

上一篇:ansible基础配置使用(一)


下一篇:GitHub客户端Desktop的安装和使用总结