using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace task3
{
public delegate void MyAlarm(int temperature); //创建委托报警
public delegate void MyDispaly(int temperature); //创建委托显示
class Heater //热水器类
{
private int temperature; //温度
public int Temperature
{
get { return temperature; }
set
{
if ((temperature >= 0) && (temperature <= 100))
temperature = value;
else
Console.WriteLine("温度有问题!显示出错!");
}
}
public event MyAlarm DoMyAlarm; //建立事件报警
public event MyDispaly DoMyDisplay; //建立事件显示
public void BoilWater(int m) //m 表示烧水时间,没分钟提高摄氏度
{
Console.WriteLine("感谢您使用热水器,现在温度为{0},\n本次烧水时间为{1}分钟。" +
"(每分钟提高10摄氏度)", this.temperature, m);
this.temperature = m * 10 + this.temperature;
if (this.temperature >= 100)
this.temperature = 100;
if (this.temperature >= 95)
{
DoMyAlarm(this.temperature);
DoMyDisplay(this.temperature);
Console.WriteLine("");
}
else
Console.WriteLine("目前还没烧开!\n");
}
}
class Alarm //报警器类
{
public void MakeAlert(int temperature)
{
Console.WriteLine("叮叮叮,叮叮叮。。。\n报警器提示水壶水温现在是{0}",temperature);
}
}
class Display //显示器类
{
public void ShowMsg(int temperature)
{
Console.WriteLine("显示器提示现在的水温为{0}", temperature);
}
}
class Program
{
static void Main(string[] args)
{
Heater he1 = new Heater(); //第一次烧水(水伟烧开,不会触发事件)
he1.Temperature = 10;
Alarm al1 = new Alarm();
Display di1 = new Display();
he1.DoMyAlarm += al1.MakeAlert; //建立关联
he1.DoMyDisplay += di1.ShowMsg; //建立关联
he1.BoilWater(1);
Heater he2 = new Heater(); //第二次烧水(水烧开,触发显示与报警事件)
he2.Temperature = 10;
Alarm al2 = new Alarm();
Display di2 = new Display();
he2.DoMyAlarm += al2.MakeAlert; //建立关联
he2.DoMyDisplay += di2.ShowMsg; //建立关联
he2.BoilWater(10);
Console.Read();
}
}
}