Files

54 lines
1.3 KiB
C#
Raw Permalink Normal View History

2025-09-15 17:07:13 +08:00
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!");
}
}
}