diff --git a/C#核心/C#核心.sln b/C#核心/C#核心.sln index 4f6f708..da50bb4 100644 --- a/C#核心/C#核心.sln +++ b/C#核心/C#核心.sln @@ -11,12 +11,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson10_封装-运算符 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson11_封装-内部类与外部类", "Lesson11_封装-内部类与外部类\Lesson11_封装-内部类与外部类.csproj", "{97B96262-E4CC-4A7B-8545-D58984CCBD70}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson13_继承-李氏替换原则", "Lesson13_继承-李氏替换原则\Lesson13_继承-李氏替换原则.csproj", "{06C184C2-C60B-4CFC-B3B8-7A8E0754D725}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson13_继承-里氏替换原则", "Lesson13_继承-李氏替换原则\Lesson13_继承-里氏替换原则.csproj", "{06C184C2-C60B-4CFC-B3B8-7A8E0754D725}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson12_继承-继承的基本概念练习", "Lesson12_继承-继承的基本概念练习\Lesson12_继承-继承的基本概念练习.csproj", "{B432065F-FFA2-4866-BC75-8AC33869B92F}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson12_继承-继承的基本概念", "Lesson12_继承-继承的基本概念\Lesson12_继承-继承的基本概念.csproj", "{45F37FBC-59A8-4FC9-B0F5-5CEA222D8C7E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson13_继承-里氏替换原则练习", "Lesson13_继承-里氏替换原则练习\Lesson13_继承-里氏替换原则练习.csproj", "{B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +53,10 @@ Global {45F37FBC-59A8-4FC9-B0F5-5CEA222D8C7E}.Debug|Any CPU.Build.0 = Debug|Any CPU {45F37FBC-59A8-4FC9-B0F5-5CEA222D8C7E}.Release|Any CPU.ActiveCfg = Release|Any CPU {45F37FBC-59A8-4FC9-B0F5-5CEA222D8C7E}.Release|Any CPU.Build.0 = Release|Any CPU + {B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/C#核心/Lesson13_继承-李氏替换原则/Lesson13_继承-李氏替换原则.csproj b/C#核心/Lesson13_继承-李氏替换原则/Lesson13_继承-里氏替换原则.csproj similarity index 100% rename from C#核心/Lesson13_继承-李氏替换原则/Lesson13_继承-李氏替换原则.csproj rename to C#核心/Lesson13_继承-李氏替换原则/Lesson13_继承-里氏替换原则.csproj diff --git a/C#核心/Lesson13_继承-李氏替换原则/Program.cs b/C#核心/Lesson13_继承-李氏替换原则/Program.cs index 21a5b57..7251d4a 100644 --- a/C#核心/Lesson13_继承-李氏替换原则/Program.cs +++ b/C#核心/Lesson13_继承-李氏替换原则/Program.cs @@ -1,10 +1,115 @@ namespace Lesson13_继承_李氏替换原则 { + + #region 知识点一 基本概念 + //里氏替换原则是面向对象七大原则中最重要的原则 + //概念: + //任何父类出现的地方,子类都可以替代 + //重点: + //语法表现——父类容器装子类对象,因为子类对象包含了父类的所有内容 + //作用: + //方便进行对象存储和管理 + #endregion + + #region 知识点二 基本实现 + class GameObject + { + + } + class Player:GameObject + { + public void PlayerAtk() + { + Console.WriteLine("玩家攻击"); + } + } + class Monster :GameObject + { + public void MonsterAtk() + { + Console.WriteLine("怪物攻击"); + } + } + class Boss:GameObject + { + public void BossAtk() + { + Console.WriteLine("Boss攻击"); + } + } + #endregion + + internal class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); + //里氏替换原则 + GameObject player = new Player(); + GameObject monster = new Monster(); + GameObject boss = new Boss(); + + GameObject[] objects = new GameObject[] { player, monster, boss }; + + //player.atk();//容器是gameobject,没有atk这个方法 + + #region 知识点三 is和as + //基本概念 + //is:判断一个对象是否执行类对象 + //返回时:bool 是为真 不是为假 + if (player is Player) + { + + }else if (player is Monster) + { + + } + + //as:将一个对象转换为指定类对象 + //返回值:指定类型对象 + //成功返回指定类型对象,失败返回null + Player test = player as Player;//将player转换为Player类型对象(可)返回player类型对象(Player 类型的 test,存放player) + Player test2 = monster as Player;//将monster转换为Player类型对象(报错)返回空(Player 类型的 test2, 存放的是空) + + + //基本语法 + // 类对象 is 类名 该语句块 会有一个bool返回值 true和false + // 类对象 as 类名 该语句块 会有一个对象返回值 对象和null + // 一般组合使用 + if(player is Player) + { + //常规写法 + //Player p1 = player as Player; + //p1.PlayerAtk(); + + //一句话写法 + (player as Player).PlayerAtk(); + } + //同时声明多个子类对象,并集中按需使用 ->GameObject[] objects = new GameObject[] { player, monster, boss }; + for (int i = 0; i < objects.Length; i++) + { + if (objects[i] is Player) + { + //调用Player需要的逻辑 + //eg: + (objects[i] as Player).PlayerAtk(); + } + else if (objects[i] is Monster) + { + //调用Monster需要的逻辑 + //eg: + (objects[i] as Monster).MonsterAtk(); + } + else if (objects[i] is Boss) + { + //调用Boss需要的逻辑 + //eg: + (objects[i] as Boss).BossAtk(); + } + } + + #endregion } } } diff --git a/C#核心/Lesson13_继承-里氏替换原则练习/Lesson13_继承-里氏替换原则练习.csproj b/C#核心/Lesson13_继承-里氏替换原则练习/Lesson13_继承-里氏替换原则练习.csproj new file mode 100644 index 0000000..e57aaeb --- /dev/null +++ b/C#核心/Lesson13_继承-里氏替换原则练习/Lesson13_继承-里氏替换原则练习.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + Lesson13_继承_里氏替换原则练习 + enable + enable + + + diff --git a/C#核心/Lesson13_继承-里氏替换原则练习/Program.cs b/C#核心/Lesson13_继承-里氏替换原则练习/Program.cs new file mode 100644 index 0000000..0510372 --- /dev/null +++ b/C#核心/Lesson13_继承-里氏替换原则练习/Program.cs @@ -0,0 +1,134 @@ +namespace Lesson13_继承_里氏替换原则练习 +{ + class Monster + { + + } + class Boss : Monster + { + public void Skill() + { + Console.WriteLine("Boss释放了技能"); + } + } + class Goblin : Monster + { + public void Atk() + { + Console.WriteLine("小怪释放了攻击"); + } + } + class Weapon + { + + } + class SMG : Weapon + { + public void Shot() + { + Console.WriteLine("SMG shot"); + } + } + class Shotgun : Weapon + { + public void Shot() + { + Console.WriteLine("Shotgun shot"); + } + } + class Pistol : Weapon + { + public void Shot() + { + Console.WriteLine("Pistol shot"); + } + } + class Knife : Weapon + { + public void Attack() + { + Console.WriteLine("Knife Attack"); + } + } + class Player + { + private Weapon nowWeapon; + + public Player() + { + nowWeapon = new Knife(); + } + public void PickUp(Weapon newWeapon) + { + nowWeapon = newWeapon; + } + public void Attack() + { + if (nowWeapon == null) + { + Console.WriteLine("你没有武器"); + }else if (nowWeapon is Knife) + { + (nowWeapon as Knife).Attack(); + }else if(nowWeapon is Pistol) + { + (nowWeapon as Pistol).Shot(); + } + else if (nowWeapon is Shotgun) + { + (nowWeapon as Shotgun).Shot(); + } + else if (nowWeapon is SMG) + { + (nowWeapon as SMG).Shot(); + } + else + { + Console.WriteLine("ERROR"); + } + } + } + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + Random r = new Random(); + + Monster[] m = new Monster[10]; + for (int i = 0; i < m.Length; i++) + { + if (r.Next(1, 101) < 50) + { + m[i] = new Boss(); + } + else + { + m[i] = new Goblin(); + } + } + for (int i = 0;i < m.Length;i++) + { + if (m[i] is Boss) + { + (m[i] as Boss).Skill(); + }else if (m[i] is Goblin) + { + (m[i] as Goblin).Atk(); + } + else + { + Console.WriteLine("Error"); + } + } + + + Console.WriteLine(); + Player p1 = new Player(); + p1.Attack(); + SMG smg1 = new SMG(); + p1.PickUp(smg1); + p1.Attack(); + } + } +} diff --git a/C#核心/test/Program.cs b/C#核心/test/Program.cs index 3751555..23c0696 100644 --- a/C#核心/test/Program.cs +++ b/C#核心/test/Program.cs @@ -1,2 +1,25 @@ // See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); +#region 知识点一 +#endregion + +#region 知识点二 +#endregion + +#region 知识点三 +#endregion + +#region 知识点四 +#endregion + +#region 知识点五 +#endregion + +#region 知识点六 +#endregion + +#region 知识点七 +#endregion + +#region 知识点八 +#endregion \ No newline at end of file