Files
Csharp/C#进阶/Lesson13_事件练习/Program.cs
T

53 lines
1.2 KiB
C#
Raw Normal View History

2025-10-17 11:48:41 +08:00
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();
}
}
}