NO IMAGE

大家好!好久沒有上來了!小生在此向各方豪傑問好!
今天溫習了一下C#,解決了老大一個難題,以前總是搞不懂過載運算子,也不知道它有什麼用處,
今天總算把它給用上了,真有種在深山老林裡用上電得感覺,呵呵!我用它做了一個計時器,下面
我就把程式碼帖出來供大家玩樂一下也好!先把步驟說一下:
1.用.net建立一個Windows應用程式
2.在專案下新增一個類命名為“Time“
3.編寫“Time“類得實現程式碼
4.在窗體中放兩個Label控制元件和一個時鐘(Timer)控制元件
5.編寫窗體得Load事件和(Timer)控制元件的Tick事件

以下是“Time“類得實現程式碼:
public class Time
 {
  private int hours;  //小時
  private int minutes; //分鐘
  private int seconds; //秒鐘
  
  public Time()
  {
   this.hours = 0;
   this.minutes = 0;
   this.seconds = 0;
  }

  public Time(int hours,int minutes,int seconds)
  {
   this.hours = hours;
   this.minutes = minutes;
   this.seconds = seconds;
  }

  public void SetHours(int hours)
  {
   this.hours = hours;
  }

  public void SetMinutes(int minutes)
  {
   this.minutes = minutes;
  }

  public void SetSeconds(int seconds)
  {
   this.seconds = seconds;
  }

  public int GetHours()
  {
   return this.hours;
  }

  public int GetMinutes()
  {
   return this.minutes;
  }

  public int GetSeconds()
  {
   return this.seconds;
  }

  public static Time operator (Time time)
  {
   time.seconds ;
   if (time.seconds >= 60)
   {
    time.minutes ;
    time.seconds = 0;
    if (time.minutes >= 60)
    {
     time.hours ;
     time.minutes = 0;
     time.seconds = 0;
     if (time.hours >= 24)
     {
      time.hours = 0;
      time.minutes = 0;
      time.seconds = 0;
     }
    }
   }
   return new Time(time.hours,time.minutes,time.seconds);
  }
 }

以下是Load事件和Timer控制元件的Tick事件處理程式:
private void Form1_Load(object sender, System.EventArgs e)
  {
   DateTime dtime = DateTime.Now;
   int hour = dtime.Hour;
   int minute = dtime.Minute;
   int second = dtime.Second;
   time = new Time(hour,minute,second);
   this.label1.Text = “當前時間是:” hour “:” minute “:” second;
   this.timer1.Start();
  }
private void timer1_Tick(object sender, System.EventArgs e)
  {
   time ;
   this.label2.Text = “時間在流逝:” time.GetHours() “:” time.GetMinutes() “:” time.GetSeconds();
  }

時間有限不說了,執行一下看看!regular_smile.gif