private void timeExpired()//定时器超时操作
{
this.isOn = false;
this.stopTiming();//计时器停止计时
TimerCount();//通知外部监听者更新时间
TimerExpired();//触发TimerExpired事件,通知所有预定定时器到时事件的监听者
}
public void setTimerToZero()//定时器清零操作
{
this.timeRemaining = 0;
TimerCount();
}
public void TurnOn()//启动定时器
{
if (!this.isOn && this.timeRemaining > 0)
{
this.isOn = true;
this.startTiming();//计时器开始计时
TimerOn();//触发TimerOn事件,通知所有预定定时器启动事件的监听者
}
}
public void TurnOff()//停止定时器
{
if (this.isOn)
{
this.isOn = false;
if (this.isSuspended)
{
this.isSuspended = false;
}
this.stopTiming();//计时器停止计时
this.setTimerToZero();//设定时间清零
TimerOff();//触发TimerOff事件,通知所有预定定时器停止事件的监听者
}
}
public void Pause()//定时器暂停
{
if (!this.isSuspended && this.isOn)
{
this.isSuspended = true;
this.stopTiming();//计时器停止计时
TimerPause();//触发TimerPause事件,通知所有预定定时器暂停事件的监听者
}
}
public void Resume()//定时器继续计时
{
if (this.isSuspended && this.isOn)
{
this.isSuspended = false;
this.startTiming();//计时器开始计时
TimerResume();//触发TimerResume事件,通知所有预定定时器继续事件的监听者
}
}
public void Add5SecToTimer()//定时器剩余时间增加5秒
{
if (this.timeRemaining < 1800)
{
this.timeRemaining += 5;
TimerCount();
}
}
}
}
5.2将Display类映射成代码
//定义Time结构类型
public struct Time
{
public int minute;
public int second;
}
//单实例类Display
public class Display
{
private static Display instance; //静态实例引用
private int timeRemaining; //剩余时间
private Time reading; //显示器示数
private bool isPowerTubeOn; //功率管显示状态
private Display()//私有的构造函数,不能从类外部进行构造
{
this.timeRemaining = 0;
this.reading.minute = 0;
this.reading.second = 0;
this.isPowerTubeOn = false;
}
public static Display GetInstance()//返回唯一实例
{
if (instance == null)
{
instance = new Display();
}
return instance;
}
public void SetTimeRemaining(int timeRemaining) //设置剩余时间
{
this.timeRemaining = timeRemaining;
this.ProcessReading();
}
private void ProcessReading()//将剩余时间转换为显示器示数
{
this.reading.minute = timeRemaining / 60;
this.reading.second = timeRemaining % 60;
}
public Time GetReading()//提取读数
{
return this.reading;
}
public void SetIsPowerTubeOn(bool isPowerTubeOn) //设置功率管显示状态
{
this.isPowerTubeOn = isPowerTubeOn;
}
public bool GetIsPowerTubeOn()//返回功率管显示状态
{
return this.isPowerTubeOn;
<< 上一页 [11] [12] [13] [14] [15] 下一页