Net 面试题系列

xiaoxiao2021-02-28  72

Net 面试题系列(二)--代码题  一、用C#实现如下场景: 猫大叫一声,所有的老鼠都开始 逃跑,主人被惊醒 要求: 1、要有联动性,老鼠和主人的行为是被动的 2、要考虑可扩展性,猫的叫声可能引起其他联动效应 分析 :这主要 考的事委托 和事件,相关知识点 要 多去了解 代码如下 : public sealed class Cat  //猫类     {         public event EventHandler Calling;         public void Call()         {             Console.WriteLine("猫开始叫了……");             if (Calling != null)              //检查事件是否被注册                 Calling(this, EventArgs.Empty);//调用事件注册的方法         }     }     public sealed class Mouse //老鼠类     {         public void Escape(object sender, EventArgs e)         {             Console.WriteLine("老鼠逃跑了...");         }     }     public sealed class Master //主人     {         public void Wakened(object sender, EventArgs e)         {             Console.WriteLine("主人惊醒了...");         }     }      //Main 函数中调用:  static void Main(string[] args)  {         Cat cat = new Cat();             Mouse mouse = new Mouse();             Master master = new Master();             cat.Calling += new EventHandler(mouse.Escape);             cat.Calling += new EventHandler(master.Wakened);             cat.Call(); }   二、用C#实现冒泡排序             int[] scores = { 10, 20, 60, 40 };             for (int i = 0; i < scores.Length - 1; i++)   //控制比较的圈数             {                 for (int j = 0; j < scores.Length - i - 1; j++)                 {                     if (scores[j] > scores[j + 1])                     {                         int temp = scores[j];                         scores[j] = scores[j + 1];                         scores[j + 1] = temp;                     }                 }             }             for (int i = 0; i < scores.Length; i++)             {                 Console.WriteLine(scores[i]);             }

转载请注明原文地址: https://www.6miu.com/read-46804.html

最新回复(0)