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!"); + } + } +}