添加Lesson13
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user