添加Lesson19

This commit is contained in:
2025-09-14 16:31:09 +08:00
parent c16355e935
commit 372d55923a
5 changed files with 408 additions and 0 deletions
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Lesson19_多态_接口</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+189
View File
@@ -0,0 +1,189 @@
namespace Lesson19_多态_接口
{
#region
//接口是行为的抽象规范
//他也是一种自定义类型
//关键字 interface
//接口声明的规范
//1.不包含成员变量
//2.只包含方法、属性、索引器、事件
//3.成员不能被实现
//4.成员可以不用写访问修饰符,不能是私有的
//5.接口不能继承类,但是可以继承另一个接口
//接口的使用规范
//1.类可以继承多个接口
//2.类继承接口后,必须实现接口中所有成员
//特点:
//1.他和类的声明类似
//2.接口是用来继承的
//3.接口不能被实例化,但是可以作为容器存储对象
#endregion
#region
//接口的关键字:interface
//语法:
//interface 接口名
//{
//}
//一句话记忆:接口是抽象行为的基类
//接口命名规范 帕斯卡前面加个I
interface IFly
{
//int a;不能包含成员变量
void Fly();//可以加public和protect,不能加private这样违反了上面使用规范知识点的2
string Name
{
get;
set;
}
int this[int index]
{
get;
set;
}
event Action doSomthing;//先不学事件声明
}
#endregion
#region 使
class Animal
{
}
//接口用来继承
//1.类可以继承1个类,n个接口
//2.继承了接口后 必须实现其中的内容 并且必须是public的
class Human : Animal, IFly
{
public int this[int index]
{
get
{
return 0;
}
set
{
this[index] = value;
}
}
public string Name
{
get;
set;
}
public event Action doSomthing;
//3.实现的接口函数,可以通过加virtual再在子类中重写
//4.接口也遵循里氏替换原则
public virtual void Fly()
{
Console.WriteLine("人飞");
}
}
#endregion
#region
//接口继承接口时 不需要实现
//待类继承接口后 类自己去实现所有内容
interface IWalk
{
void Walk();
}
interface IMove : IFly,IWalk
{
void Move();
}
class Test : IMove
{
public int this[int index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public string Name { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public event Action doSomthing;
public void Fly()
{
throw new NotImplementedException();
}
public void Move()
{
throw new NotImplementedException();
}
public void Walk()
{
throw new NotImplementedException();
}
}
#endregion
#region
//当一个类继承两个接口
//但是接口中存在着同名方法时
//注意:显示实现接口时 不能写访问修饰符
interface IAtk
{
void Atk();
}
interface ISuperAtk
{
void Atk();
}
class Player : ISuperAtk, IAtk
{
void ISuperAtk.Atk()//显示实现接口就是 接口名.方法
{
throw new NotImplementedException();
}
void IAtk.Atk()
{
throw new NotImplementedException();
}
public void Atk()
{
throw new NotImplementedException();
}
}
#endregion
internal class Program
{
static void Main(string[] args)
{
//IFly f = new IFly();不能被实例化
IFly f = new Human();//可以用里氏替换原则
f.Fly();
IMove m = new Test();
IFly f2 = new Test();
IWalk w = new Test();
IAtk ia = new Player();
ISuperAtk isa = new Player();
Player p = new Player();
(p as IAtk).Atk();
(p as ISuperAtk).Atk();
p.Atk();
}
}
// 总结:
// 继承类:是对象间的继承,包括特征行为等等
// 继承接口:是行为间的继承,继承接口的行为规范,按照规范去实现内容
// 由于接口也是遵循里氏替换原则,所以可以用接口容器装对象
// 那么久可以实现 装载各种毫无关系但是却有相同行为的对象
// 注意:
//1. 接口值包含 成员方法、属性、索引器、事件,并且都不实现,都没有访问修饰符
//2. 可以继承多个接口,但是只能继承一个类
//3. 接口可以继承接口,相当于在进行行为合并,待子类继承时再去实现具体的行为
//4. 接口可以被显示实现 主要用于实现不同接口中的同名函数的不同表现
//5. 实现的接口方法 可以加 virtual 关键字 之后子类 再重写
}