C# 面试题(三)

1、下面代码的输出结果是什么

using System;
class A
{
public A()
{
PrintFields();
}
public virtual void PrintFields(){}
}
class B:A
{
int x=1;
int y;
public B()
{
y=-1;
}
public override void PrintFields()
{
Console.WriteLine("x={0},y={1}",x,y);
}

答:X=1,Y=0;x=1 y = -1

2、有一个字符串 "I am a good man",设计一个函数,返回 "man good a am I"。

static string Reverse()    
      {    
             string s = "I am a good man";    
             string[] arr = s.Split(' ');    
             string res = "";    
             for (int i = arr.Length - 1; i >= 0; i--)    
             {    
                 res += arr[i];    
                 if (i > 0)    
                     res += " ";    
             }    
             return res;    
         } 

3、C# 九九乘法表算法实现

static void Mu()    
          {    
              string t = string.Empty;    
              for (int i = 1; i < 10; i++)    
              {    
                  for (int j = 1; j <= i; j++)    
                  {    
                      t = string.Format("{0}*{1}={2} ", j, i, (j * i));    
                      Console.Write(t);    
                      //if (j * i < 82)    
                      //    Console.Write(" ");    
                      if (i == j)    
                          Console.Write("\n");    
                  }    
              }    
          } 

4、代码实现猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。

namespace DelegateEvent  
    {  
        public delegate void SubEventHandler();  
        public abstract class Subject  
        {  
            public event SubEventHandler SubEvent;  
            protected void FireAway()   //开火, 抽象类可以有具体方法。  
            {  
                if (this.SubEvent != null)  
                    this.SubEvent();  
            }  
        }  
          
        public class Cat:Subject  
        {  
            public void Cry()  
            {  
                Console.WriteLine("cat cryed.")  
                this.FireAway();  
            }  
        }  
      
        public abstract class Observer  //定义一个观察者的抽象类,这样的类有一点就是观察谁,这个谁肯定是一个类,这里指猫  
        {  
            public Observer(Subject sub)  //抽象类也可以定义构造函数  
            {   
                sub.SubEvent +=new SubEventHandler(Respose);   //注册猫叫事件(表达有点含糊),当此事件触发的时候,老鼠会做出回应  
            }  
            public abstract void Respose();   
        }  
          
        //定义一个观察者,老鼠  
        public class Mouse : Observer  
        {  
            private string name;  
            public Mouse(string name, Subject sub)  //定义构造函数,并初始化父类  
                : base(sub)  
            {  
                this.name = name;  
            }  
      
            public override void Respose()  
            {  
                Console.WriteLine(name+" attempt to escape!");  
            }  
        }  
        //定义一个观察者,主人  
        public class Master : Observer  
        {  
            public Master(Subject sub) : base(sub) { }  
            public override void Respose()  
            {  
                Console.WriteLine("host waken");  
            }  
        }  
      
        class Program  
        {  
            static void Main(string[] args)  
            {  
                Cat cat = new Cat();  
                Mouse mouse1 = new Mouse("mouse1", cat); //在对象初始化的时候,已经注册了对猫叫的响应事件  
                Mouse mouse2 = new Mouse("mouse2",cat);  
                Master master = new Master(cat);  
                cat.Cry();  
                Console.Read();  
            }  
        }  
    }  

 

posted @ 2018-05-08 22:46  爱生活,爱代码  阅读(157)  评论(0编辑  收藏  举报