From 110ac84507d2ef6f3cf0d3346e0752b2555f705b Mon Sep 17 00:00:00 2001 From: Kister Hakusan <2753888203@qq.com> Date: Mon, 15 Sep 2025 17:07:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Lesson20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Lesson20_多态-密封方法.csproj | 11 ++++ C#核心/Lesson20_多态-密封方法/Program.cs | 53 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 C#核心/Lesson20_多态-密封方法/Lesson20_多态-密封方法.csproj create mode 100644 C#核心/Lesson20_多态-密封方法/Program.cs diff --git a/C#核心/Lesson20_多态-密封方法/Lesson20_多态-密封方法.csproj b/C#核心/Lesson20_多态-密封方法/Lesson20_多态-密封方法.csproj new file mode 100644 index 0000000..51ae074 --- /dev/null +++ b/C#核心/Lesson20_多态-密封方法/Lesson20_多态-密封方法.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + Lesson20_多态_密封方法 + enable + enable + + + diff --git a/C#核心/Lesson20_多态-密封方法/Program.cs b/C#核心/Lesson20_多态-密封方法/Program.cs new file mode 100644 index 0000000..de16bbf --- /dev/null +++ b/C#核心/Lesson20_多态-密封方法/Program.cs @@ -0,0 +1,53 @@ +namespace Lesson20_多态_密封方法 +{ + internal class Program + { + #region 知识回顾 + //用sealed来防止类被继续继承 + #endregion + + #region 知识点一 密封方法的基本概念 + //用密封关键字sealed修饰的重写函数 + //作用:让虚方法或者抽象方法之后不能被重写 + //特点:和override一起出现 + #endregion + + + #region 知识点二 实例 + abstract class Animal + { + public string name; + public abstract void Eat(); + public virtual void Speak() { + Console.WriteLine("123"); + } + } + class Person : Animal + { + public override void Eat() + { + Console.WriteLine("123"); + } + public override void Speak() + { + Console.WriteLine("spaek"); + } + } + class WhitePerson : Person + { + public sealed override void Speak() + { + base.Speak(); + } + public sealed override void Eat() + { + base.Eat(); + } + } + #endregion + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +}