WEB开发网
开发学院软件开发C语言 C#3.0笔记(二)预备知识之委托与事件 阅读

C#3.0笔记(二)预备知识之委托与事件

 2009-05-18 08:28:07 来源:WEB开发网   
核心提示: 完整代码:publicclassEventCar{publicdelegatevoidCarEventHandler(stringmsg);publiceventCarEventHandlerAbortToBlow;publiceventCarEventHandlerExploded;pr

完整代码:

  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);
      }
    }

  }

上一页  1 2 3 4 5  下一页

Tags:笔记 预备 知识

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接