添加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
+12
View File
@@ -53,6 +53,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson12_委托", "Lesson12
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson12_委托练习", "Lesson12_委托练习\Lesson12_委托练习.csproj", "{A5EDC63B-0ACF-48FF-BABC-8C4D1B0E12D9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson13_事件", "Lesson13_事件\Lesson13_事件.csproj", "{66C78CE0-E022-4E59-8EB4-C469957AA5D9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson13_事件练习", "Lesson13_事件练习\Lesson13_事件练习.csproj", "{AD35AAD1-3AEE-4A8D-95EB-AA3DAE7776E0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -159,6 +163,14 @@ Global
{A5EDC63B-0ACF-48FF-BABC-8C4D1B0E12D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5EDC63B-0ACF-48FF-BABC-8C4D1B0E12D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5EDC63B-0ACF-48FF-BABC-8C4D1B0E12D9}.Release|Any CPU.Build.0 = Release|Any CPU
{66C78CE0-E022-4E59-8EB4-C469957AA5D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66C78CE0-E022-4E59-8EB4-C469957AA5D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66C78CE0-E022-4E59-8EB4-C469957AA5D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66C78CE0-E022-4E59-8EB4-C469957AA5D9}.Release|Any CPU.Build.0 = Release|Any CPU
{AD35AAD1-3AEE-4A8D-95EB-AA3DAE7776E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD35AAD1-3AEE-4A8D-95EB-AA3DAE7776E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD35AAD1-3AEE-4A8D-95EB-AA3DAE7776E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD35AAD1-3AEE-4A8D-95EB-AA3DAE7776E0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -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>
+102
View File
@@ -0,0 +1,102 @@
namespace Lesson13_事件
{
#region
//事件是基于委托的存在
//事件是委托的安全包裹
//让委托的使用具有安全性
//事件 是一种特殊的变量类型
#endregion
#region 使
//声明语法:
//访问修饰符 event 委托类型 事件名称;
//事件的使用:
//1.事件是作为 成员变量存在于类中
//2.委托怎么用 事件就怎么用
//事件相对于委托的区别
//1.不能在类外部 赋值
//2.不能在类外部 调用
//注意:
//他只能作为成员存在于类和接口以及结构体中
class Test
{
//委托成员变量 用于存储 函数
public Action myFun;
//事件成员变量 用于存储 函数
public event Action myEvent;
public Test()
{
//事件的使用和委托 一模一样 只是有些细微的区别
myFun = TestFun;
myFun += TestFun;
myFun.Invoke();
myFun();
myFun = null;
myEvent = TestFun;
myEvent += TestFun;
myEvent.Invoke();
myEvent();
myEvent = null;
}
public void TestFun()
{
Console.WriteLine("TestFun");
}
public void DoEvent()
{
if (myEvent != null)
{
myEvent();
}
}
}
#endregion
#region
#endregion
class Program
{
static void Main(string[] args)
{
Test t = new Test();
//委托可以在外部赋值
t.myFun += TestFunOut;
t.myFun = null;
//事件不能在外部赋值
//但事件可以在外部添加函数
//t.myEvent = null;//错误 事件不能在类外部赋值
//t.myEvent = TestFunOut;//错误 事件不能在类外部赋值
t.myEvent += TestFunOut;
t.myEvent -= TestFunOut;//正确
//事件不能在外部调用
t.myFun();
t.myFun.Invoke();
//t.myEvent();//错误 事件不能在类外部调用
//t.myEvent.Invoke();//错误 事件不能在类外部调用
//如果需要在外部调用,那么最好在类中提供一个调用事件的函数
t.DoEvent();
Action a = TestFunOut;
//event Action a1 = TestFunOut;//
//总结
//事件是对委托的安全包裹
}
static void TestFunOut()
{
Console.WriteLine("TestFunOut");
}
}
}
@@ -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>
+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();
}
}
}