添加Lesson19
This commit is contained in:
@@ -39,6 +39,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson18_多态-抽象类
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson18_多态-抽象类和抽象方法练习", "Lesson18_多态-抽象类和抽象方法练习\Lesson18_多态-抽象类和抽象方法练习.csproj", "{7004726B-5B33-4B2A-ADDB-EF22DA593630}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson19_多态-接口", "Lesson19_多态-接口\Lesson19_多态-接口.csproj", "{FC39DDE9-AC80-40A2-9CCA-DC06510A8881}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson19_多态-接口练习", "Lesson19_多态-接口练习\Lesson19_多态-接口练习.csproj", "{D511EDF3-7769-45BB-8304-3081918FA633}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -117,6 +121,14 @@ Global
|
||||
{7004726B-5B33-4B2A-ADDB-EF22DA593630}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7004726B-5B33-4B2A-ADDB-EF22DA593630}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7004726B-5B33-4B2A-ADDB-EF22DA593630}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{FC39DDE9-AC80-40A2-9CCA-DC06510A8881}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{FC39DDE9-AC80-40A2-9CCA-DC06510A8881}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{FC39DDE9-AC80-40A2-9CCA-DC06510A8881}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{FC39DDE9-AC80-40A2-9CCA-DC06510A8881}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{D511EDF3-7769-45BB-8304-3081918FA633}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D511EDF3-7769-45BB-8304-3081918FA633}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D511EDF3-7769-45BB-8304-3081918FA633}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D511EDF3-7769-45BB-8304-3081918FA633}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -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>
|
||||
@@ -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 关键字 之后子类 再重写
|
||||
}
|
||||
@@ -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>
|
||||
@@ -0,0 +1,185 @@
|
||||
using Lesson19_多态_接口练习;
|
||||
|
||||
namespace Lesson19_多态_接口练习
|
||||
{
|
||||
interface ICheck
|
||||
{
|
||||
public void Check();
|
||||
}
|
||||
class Person : ICheck
|
||||
{
|
||||
public void Check()
|
||||
{
|
||||
Console.WriteLine("派出所登记");
|
||||
}
|
||||
}
|
||||
class Car
|
||||
{
|
||||
public void Check()
|
||||
{
|
||||
Console.WriteLine("车管所登记");
|
||||
}
|
||||
}
|
||||
class House
|
||||
{
|
||||
public void Check()
|
||||
{
|
||||
Console.WriteLine("房管局登记");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
interface IFly
|
||||
{
|
||||
public void Fly();
|
||||
}
|
||||
interface IWalk
|
||||
{
|
||||
public void Walk();
|
||||
}
|
||||
interface ISwim
|
||||
{
|
||||
public void Swim();
|
||||
}
|
||||
|
||||
class Sparrow : IFly, IWalk
|
||||
{
|
||||
public void Fly()
|
||||
{
|
||||
Console.WriteLine("麻雀飞");
|
||||
}
|
||||
public void Walk()
|
||||
{
|
||||
Console.WriteLine("麻雀走");
|
||||
}
|
||||
}
|
||||
class Ostrich : IWalk
|
||||
{
|
||||
public void Walk()
|
||||
{
|
||||
Console.WriteLine("鸵鸟走");
|
||||
}
|
||||
}
|
||||
class Penguin : IWalk, ISwim
|
||||
{
|
||||
public void Walk()
|
||||
{
|
||||
Console.WriteLine("企鹅走");
|
||||
}
|
||||
public void Swim()
|
||||
{
|
||||
Console.WriteLine("企鹅游泳");
|
||||
}
|
||||
}
|
||||
class Parrot : IFly, IWalk
|
||||
{
|
||||
public void Fly()
|
||||
{
|
||||
Console.WriteLine("鹦鹉飞");
|
||||
}
|
||||
public void Walk()
|
||||
{
|
||||
Console.WriteLine("走");
|
||||
}
|
||||
}
|
||||
class Helicopter : IFly
|
||||
{
|
||||
public void Fly()
|
||||
{
|
||||
Console.WriteLine("直升机飞");
|
||||
}
|
||||
}
|
||||
class Swan : IFly, IWalk, ISwim
|
||||
{
|
||||
public void Fly()
|
||||
{
|
||||
Console.WriteLine("天鹅飞");
|
||||
}
|
||||
public void Walk()
|
||||
{
|
||||
Console.WriteLine("天鹅走");
|
||||
}
|
||||
public void Swim()
|
||||
{
|
||||
Console.WriteLine("天鹅游泳");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
interface IUSB
|
||||
{
|
||||
void ReadData();
|
||||
}
|
||||
|
||||
class StorageDevice : IUSB
|
||||
{
|
||||
public string name;
|
||||
public StorageDevice(string name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
public void ReadData()
|
||||
{
|
||||
Console.WriteLine("USB{0}传输数据",name);
|
||||
}
|
||||
}
|
||||
|
||||
class MP3 : IUSB
|
||||
{
|
||||
public void ReadData()
|
||||
{
|
||||
Console.WriteLine("MP3传输数据");
|
||||
}
|
||||
}
|
||||
class Computer
|
||||
{
|
||||
public IUSB usb1;
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
Person p1= new Person();
|
||||
p1.Check();
|
||||
Car c1 = new Car();
|
||||
c1.Check();
|
||||
House h1 = new House();
|
||||
h1.Check();
|
||||
|
||||
Sparrow s1 = new Sparrow();
|
||||
s1.Walk();
|
||||
s1.Fly();
|
||||
Ostrich o1 = new Ostrich();
|
||||
o1.Walk();
|
||||
Penguin p2 = new Penguin();
|
||||
p2.Swim();
|
||||
p2.Walk();
|
||||
Parrot p3 = new Parrot();
|
||||
p3.Walk();
|
||||
p3.Fly();
|
||||
Helicopter h2 = new Helicopter();
|
||||
h2.Fly();
|
||||
Swan s3 = new Swan();
|
||||
s3.Walk();
|
||||
s3.Fly();
|
||||
s3.Swim();
|
||||
|
||||
StorageDevice hd = new StorageDevice("移动硬盘");
|
||||
StorageDevice ud = new StorageDevice("U盘");
|
||||
MP3 mp3 = new MP3();
|
||||
Computer computer = new Computer();
|
||||
|
||||
computer.usb1 = hd;
|
||||
computer.usb1.ReadData();
|
||||
|
||||
computer.usb1 = ud;
|
||||
computer.usb1.ReadData();
|
||||
|
||||
computer.usb1 = mp3;
|
||||
computer.usb1.ReadData();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user