C#3.0笔记(二)预备知识之委托与事件
2009-05-18 08:28:07 来源:WEB开发网完整代码:
public class EventCar
{
public delegate void CarEventHandler(string msg);
public event CarEventHandler AbortToBlow;
public event CarEventHandler Exploded;
private const int MaxSpeed = 180;
public int CurrSpeed { get; private set; }
public bool IsDead { get; private set; }
public string Name { get; private set; }
public EventCar() { }
public EventCar(string carName, int currSpeed)
{
if (currSpeed > MaxSpeed)
IsDead = true;
else
{
Name = carName;
CurrSpeed = currSpeed;
}
}
public void Accelerate(int addSpeed)
{
if (IsDead)
{
if (this.Exploded!= null)
Exploded("The Car Is Dead");
}
else
{
CurrSpeed += addSpeed;
if (10 == MaxSpeed - CurrSpeed &&AbortToBlow != null)
{
AbortToBlow("Careful!Bona blow!");
}
if (CurrSpeed >= MaxSpeed)
IsDead = true;
else
Console.WriteLine("CurrentSpeed:{0}", CurrSpeed);
}
}
}
更多精彩
赞助商链接