From be8eb0f4229abc7b4e791ded0c7099acb07d42fb Mon Sep 17 00:00:00 2001 From: Kister Hakusan <2753888203@qq.com> Date: Fri, 17 Oct 2025 11:48:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Lesson13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- C#进阶/C#进阶.sln | 12 +++ C#进阶/Lesson13_事件/Lesson13_事件.csproj | 10 ++ C#进阶/Lesson13_事件/Program.cs | 102 ++++++++++++++++++ .../Lesson13_事件练习/Lesson13_事件练习.csproj | 10 ++ C#进阶/Lesson13_事件练习/Program.cs | 52 +++++++++ 5 files changed, 186 insertions(+) create mode 100644 C#进阶/Lesson13_事件/Lesson13_事件.csproj create mode 100644 C#进阶/Lesson13_事件/Program.cs create mode 100644 C#进阶/Lesson13_事件练习/Lesson13_事件练习.csproj create mode 100644 C#进阶/Lesson13_事件练习/Program.cs diff --git a/C#进阶/C#进阶.sln b/C#进阶/C#进阶.sln index 00e563e..7213416 100644 --- a/C#进阶/C#进阶.sln +++ b/C#进阶/C#进阶.sln @@ -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 diff --git a/C#进阶/Lesson13_事件/Lesson13_事件.csproj b/C#进阶/Lesson13_事件/Lesson13_事件.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson13_事件/Lesson13_事件.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson13_事件/Program.cs b/C#进阶/Lesson13_事件/Program.cs new file mode 100644 index 0000000..dbdc192 --- /dev/null +++ b/C#进阶/Lesson13_事件/Program.cs @@ -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"); + } + } +} diff --git a/C#进阶/Lesson13_事件练习/Lesson13_事件练习.csproj b/C#进阶/Lesson13_事件练习/Lesson13_事件练习.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson13_事件练习/Lesson13_事件练习.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson13_事件练习/Program.cs b/C#进阶/Lesson13_事件练习/Program.cs new file mode 100644 index 0000000..548cb33 --- /dev/null +++ b/C#进阶/Lesson13_事件练习/Program.cs @@ -0,0 +1,52 @@ +namespace Lesson13_事件练习 +{ + class Heater + { + public event Action 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(); + } + } +}