今天去火车站送女朋友了
今天天气很不错,本来不应该去送女朋友。但是我总是感觉不去送会后悔,或许自己有强迫症。6:30就起床了,然后打车去女朋友家。收拾好行李,匆忙地就去了火车站。这是第一次去送女朋友,我知道,以后有很多次。很多次
昨天看了仔细看了一下delegate,感觉非常不错也:),这里提供一个简单例子:
此处介绍的示例是建立在事件和委托和引发事件中讨论的多个部分的基础上的。
本示例显示了如何从您的类引发事件以及如何处理事件。它定义了以下类。
AlarmClock 是引发 Alarm 事件的类。
AlarmEventArgs 为 Alarm 事件定义数据。
AlarmEventHandler 是 Alarm 事件的委托。
WakeMeUp 是具有 AlarmRang 方法的类,该方法处理 Alarm 事件。
AlarmDriver 是示范事件如何连结的类。该类实例化 AlarmClock 和 WakeMeUp。然后,该类通过对 WakeMeUp 实例的 AlarmRang 方法的引用实例化 AlarmEventHandler 委托。AlarmDriver 通过向 AlarmClock 的实例注册该委托并用 += 语法将委托添加到一个事件,完成事件连结。
编译和运行该示例
将以下代码复制到源文件(如 EventSample.cs 或 EventSample.vb)。
从包含源文件的目录下,执行下列命令:
[C#]
csc /r:System.dll EventSample.cs
[Visual Basic]
vbc /r:System.dll EventSample.vb
(/r 选项引用 System 程序集。)
运行创建的可执行文件 EventSample.exe。
[C#]
// EventSample.cs.
//
namespace EventSample
{
using System;
using System.ComponentModel;
// Class that contains the data for
// the alarm event. Derives from System.EventArgs.
//
public class AlarmEventArgs : EventArgs
{
private readonly bool snoozePressed ;
private readonly int nrings;
//Constructor.
//
public AlarmEventArgs(bool snoozePressed, int nrings)
{
this.snoozePressed = snoozePressed;
this.nrings = nrings;
}
// The NumRings property returns the number of rings
// that the alarm clock has sounded when the alarm event
// is generated.
//
public int NumRings
{
get { return nrings;}
}
// The SnoozePressed property indicates whether the snooze
// button is pressed on the alarm when the alarm event is generated.
//
public bool SnoozePressed
{
get {return snoozePressed;}
}
// The AlarmText property that contains the wake-up message.
//
public string AlarmText
{
get
{
if (snoozePressed)
{
return ("Wake Up!!! Snooze time is over.");
}
else
{
return ("Wake Up!");
}
}
}
}
// Delegate declaration.
//
public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
// The Alarm class that raises the alarm event.
//
public class AlarmClock
{
private bool snoozePressed = false;
private int nrings = 0;
private bool stop = false;
// The Stop property indicates whether the
// alarm should be turned off.
//
public bool Stop
{
get {return stop;}
set {stop = value;}
}
// The SnoozePressed property indicates whether the snooze
// button is pressed on the alarm when the alarm event is generated.
//
public bool SnoozePressed
{
get {return snoozePressed;}
set {snoozePressed = value;}
}
// The event member that is of type AlarmEventHandler.
//
public event AlarmEventHandler Alarm;
// The protected OnAlarm method raises the event by invoking
// the delegates. The sender is always this, the current instance
// of the class.
//
protected virtual void OnAlarm(AlarmEventArgs e)
{
if (Alarm != null)
{
// Invokes the delegates.
Alarm(this, e);
}
}
// This alarm clock does not have
// a user interface.
// To simulate the alarm mechanism it has a loop
// that raises the alarm event at every iteration
// with a time delay of 300 milliseconds,
// if snooze is not pressed. If snooze is pressed,
// the time delay is 1000 milliseconds.
//
public void Start()
{
for (;;)
{
nrings++;
if (stop)
{
break;
}
else if (snoozePressed)
{
System.Threading.Thread.Sleep(1000);
{
AlarmEventArgs e = new AlarmEventArgs(snoozePressed,
nrings);
OnAlarm(e);
}
}
else
{
System.Threading.Thread.Sleep(300);
AlarmEventArgs e = new AlarmEventArgs(snoozePressed,
nrings);
OnAlarm(e);
}
}
}
}
// The WakeMeUp class that has a method AlarmRang that handles the
// alarm event.
//
public class WakeMeUp
{
public void AlarmRang(object sender, AlarmEventArgs e)
{
Console.WriteLine(e.AlarmText +" ");
if (!(e.SnoozePressed))
{
if (e.NumRings % 10 == 0)
{
Console.WriteLine(" Let alarm ring? Enter Y");
Console.WriteLine(" Press Snooze? Enter N");
Console.WriteLine(" Stop Alarm? Enter Q");
String input = Console.ReadLine();
if (input.Equals("Y") ||input.Equals("y")) return;
else if (input.Equals("N") || input.Equals("n"))
{
((AlarmClock)sender).SnoozePressed = true;
return;
}
else
{
((AlarmClock)sender).Stop = true;
return;
}
}
}
else
{
Console.WriteLine(" Let alarm ring? Enter Y");
Console.WriteLine(" Stop Alarm? Enter Q");
String input = Console.ReadLine();
if (input.Equals("Y") || input.Equals("y")) return;
else
{
((AlarmClock)sender).Stop = true;
return;
}
}
}
}
// The driver class that hooks up the event handling method of
// WakeMeUp to the alarm event of an Alarm object using a delegate.
// In a forms-based application, the driver class is the
// form.
//
public class AlarmDriver
{
public static void Main (string[] args)
{
// Instantiates the event receiver.
WakeMeUp w= new WakeMeUp();
// Instantiates the event source.
AlarmClock clock = new AlarmClock();
// Wires the AlarmRang method to the Alarm event.
clock.Alarm += new AlarmEventHandler(w.AlarmRang);
clock.Start();
}
}
}
本示例显示了如何从您的类引发事件以及如何处理事件。它定义了以下类。
AlarmClock 是引发 Alarm 事件的类。
AlarmEventArgs 为 Alarm 事件定义数据。
AlarmEventHandler 是 Alarm 事件的委托。
WakeMeUp 是具有 AlarmRang 方法的类,该方法处理 Alarm 事件。
AlarmDriver 是示范事件如何连结的类。该类实例化 AlarmClock 和 WakeMeUp。然后,该类通过对 WakeMeUp 实例的 AlarmRang 方法的引用实例化 AlarmEventHandler 委托。AlarmDriver 通过向 AlarmClock 的实例注册该委托并用 += 语法将委托添加到一个事件,完成事件连结。
编译和运行该示例
将以下代码复制到源文件(如 EventSample.cs 或 EventSample.vb)。
从包含源文件的目录下,执行下列命令:
[C#]
csc /r:System.dll EventSample.cs
[Visual Basic]
vbc /r:System.dll EventSample.vb
(/r 选项引用 System 程序集。)
运行创建的可执行文件 EventSample.exe。
[C#]
// EventSample.cs.
//
namespace EventSample
{
using System;
using System.ComponentModel;
// Class that contains the data for
// the alarm event. Derives from System.EventArgs.
//
public class AlarmEventArgs : EventArgs
{
private readonly bool snoozePressed ;
private readonly int nrings;
//Constructor.
//
public AlarmEventArgs(bool snoozePressed, int nrings)
{
this.snoozePressed = snoozePressed;
this.nrings = nrings;
}
// The NumRings property returns the number of rings
// that the alarm clock has sounded when the alarm event
// is generated.
//
public int NumRings
{
get { return nrings;}
}
// The SnoozePressed property indicates whether the snooze
// button is pressed on the alarm when the alarm event is generated.
//
public bool SnoozePressed
{
get {return snoozePressed;}
}
// The AlarmText property that contains the wake-up message.
//
public string AlarmText
{
get
{
if (snoozePressed)
{
return ("Wake Up!!! Snooze time is over.");
}
else
{
return ("Wake Up!");
}
}
}
}
// Delegate declaration.
//
public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
// The Alarm class that raises the alarm event.
//
public class AlarmClock
{
private bool snoozePressed = false;
private int nrings = 0;
private bool stop = false;
// The Stop property indicates whether the
// alarm should be turned off.
//
public bool Stop
{
get {return stop;}
set {stop = value;}
}
// The SnoozePressed property indicates whether the snooze
// button is pressed on the alarm when the alarm event is generated.
//
public bool SnoozePressed
{
get {return snoozePressed;}
set {snoozePressed = value;}
}
// The event member that is of type AlarmEventHandler.
//
public event AlarmEventHandler Alarm;
// The protected OnAlarm method raises the event by invoking
// the delegates. The sender is always this, the current instance
// of the class.
//
protected virtual void OnAlarm(AlarmEventArgs e)
{
if (Alarm != null)
{
// Invokes the delegates.
Alarm(this, e);
}
}
// This alarm clock does not have
// a user interface.
// To simulate the alarm mechanism it has a loop
// that raises the alarm event at every iteration
// with a time delay of 300 milliseconds,
// if snooze is not pressed. If snooze is pressed,
// the time delay is 1000 milliseconds.
//
public void Start()
{
for (;;)
{
nrings++;
if (stop)
{
break;
}
else if (snoozePressed)
{
System.Threading.Thread.Sleep(1000);
{
AlarmEventArgs e = new AlarmEventArgs(snoozePressed,
nrings);
OnAlarm(e);
}
}
else
{
System.Threading.Thread.Sleep(300);
AlarmEventArgs e = new AlarmEventArgs(snoozePressed,
nrings);
OnAlarm(e);
}
}
}
}
// The WakeMeUp class that has a method AlarmRang that handles the
// alarm event.
//
public class WakeMeUp
{
public void AlarmRang(object sender, AlarmEventArgs e)
{
Console.WriteLine(e.AlarmText +" ");
if (!(e.SnoozePressed))
{
if (e.NumRings % 10 == 0)
{
Console.WriteLine(" Let alarm ring? Enter Y");
Console.WriteLine(" Press Snooze? Enter N");
Console.WriteLine(" Stop Alarm? Enter Q");
String input = Console.ReadLine();
if (input.Equals("Y") ||input.Equals("y")) return;
else if (input.Equals("N") || input.Equals("n"))
{
((AlarmClock)sender).SnoozePressed = true;
return;
}
else
{
((AlarmClock)sender).Stop = true;
return;
}
}
}
else
{
Console.WriteLine(" Let alarm ring? Enter Y");
Console.WriteLine(" Stop Alarm? Enter Q");
String input = Console.ReadLine();
if (input.Equals("Y") || input.Equals("y")) return;
else
{
((AlarmClock)sender).Stop = true;
return;
}
}
}
}
// The driver class that hooks up the event handling method of
// WakeMeUp to the alarm event of an Alarm object using a delegate.
// In a forms-based application, the driver class is the
// form.
//
public class AlarmDriver
{
public static void Main (string[] args)
{
// Instantiates the event receiver.
WakeMeUp w= new WakeMeUp();
// Instantiates the event source.
AlarmClock clock = new AlarmClock();
// Wires the AlarmRang method to the Alarm event.
clock.Alarm += new AlarmEventHandler(w.AlarmRang);
clock.Start();
}
}
}