Cc classic interview question: the cat cries, the owner is awakened, and the mouse is run down

From , 5 Years ago, written in C#, viewed 188 times.
URL https://pastebin.vip/view/f1298750
  1. //定义猫大叫事件的代理
  2. public delegate void AlertHandler();
  3. //主人类
  4. class Human
  5. {
  6.   //主人被惊醒的方法
  7.   public void Wake()
  8.   {
  9.      Console.WriteLine("主人:死猫别叫");
  10.   }
  11. }
  12. //老鼠类
  13. class Mouse
  14. {
  15.   //老鼠被吓包的方法
  16.   public void Run()
  17.   {
  18.       Console.WriteLine("老鼠:有危险,快跑!");
  19.    }
  20. }
  21. //猫类
  22. class Cat
  23. {
  24.    //猫大叫事件
  25.    public event AlertHandler AlertEvent;
  26.    public Cat()
  27.    {
  28.    //猫大叫时执行Cry方法
  29.      AlertEvent +=new AlertHandler(Cry);
  30.    }
  31.   //猫大叫事件执行的处理程序
  32.   publicvoid Alert()
  33.   {
  34.     Console.WriteLine("猫:喵...喵...");
  35.    }
  36.   //猫大叫的方法
  37.   public void Cry()
  38.   {
  39.     //触发猫大叫的事件
  40.    AlertEvent();
  41.    }
  42. }
  43. //房子类
  44. class House
  45. {
  46.   //房子里有一只老鼠、一只猫和主人
  47.   public Mouse mouse = new Mouse();
  48.   public Cat cat = new Cat();
  49.   public Human human = new Human();
  50.   //由于在一个房子里,猫大叫的事件会引发老鼠“逃跑”和主人“惊醒”
  51.   //所以在这里把老鼠“逃跑”和主人“惊醒”两个方法挂接到猫大叫的事件上。
  52. public House()
  53. {
  54.   cat.AlertEvent +=new AlertHandler(mouse.Run);
  55.   cat.AlertEvent +=new AlertHandler(human.Wake);
  56. }
  57. }
  58. //客户程序
  59. class Program
  60. {
  61. static void Main(string[] args)
  62. {
  63. //有一间房子
  64. House h = new House();
  65. //猫大叫
  66. h.cat.Cry();
  67. }
  68. }
  69. //csharp/6570

Reply to "Cc classic interview question: the cat cries, the owner is awakened, and the mouse is run down"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website