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(); } } }