添加Lesson13

This commit is contained in:
2025-10-17 11:48:41 +08:00
parent 657a23e681
commit be8eb0f422
5 changed files with 186 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
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();
}
}
}