添加Lesson20

This commit is contained in:
2025-09-15 17:07:13 +08:00
parent 372d55923a
commit 110ac84507
2 changed files with 64 additions and 0 deletions
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Lesson20_多态_密封方法</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -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!");
}
}
}