53 lines
1.2 KiB
C#
53 lines
1.2 KiB
C#
|
|
namespace Lesson13_事件练习
|
||
|
|
{
|
||
|
|
class Heater
|
||
|
|
{
|
||
|
|
public event Action<int> Boiled;
|
||
|
|
private int temperature = 25;
|
||
|
|
public void AddHeat()
|
||
|
|
{
|
||
|
|
while (temperature != 100)
|
||
|
|
{
|
||
|
|
temperature += 1;
|
||
|
|
Console.WriteLine("目前水温:" + temperature);
|
||
|
|
Thread.Sleep(20);
|
||
|
|
if(temperature >= 95)
|
||
|
|
{
|
||
|
|
if (Boiled != null)
|
||
|
|
{
|
||
|
|
Boiled(temperature);
|
||
|
|
}
|
||
|
|
Boiled = null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
class Alarm
|
||
|
|
{
|
||
|
|
public void ShowInfo(int value)
|
||
|
|
{
|
||
|
|
Console.WriteLine("报警器:" + value + "度");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
class Displayer
|
||
|
|
{
|
||
|
|
public void Showinfo(int value)
|
||
|
|
{
|
||
|
|
Console.WriteLine("显示器:" + value + "度");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
class Program
|
||
|
|
{
|
||
|
|
static void Main(string[] args)
|
||
|
|
{
|
||
|
|
Heater h = new Heater();
|
||
|
|
Alarm a = new Alarm();
|
||
|
|
Displayer d = new Displayer();
|
||
|
|
h.Boiled += a.ShowInfo;
|
||
|
|
h.Boiled += d.Showinfo;
|
||
|
|
|
||
|
|
h.AddHeat();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|