添加Lesson20
This commit is contained in:
@@ -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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user