• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
繁星
不要浪费时间
博客园    首页    新随笔    联系   管理    订阅  订阅

.NET(C#)笔试题 答案

1、一列数的规则如下: 1、1、2、3、5、8、13、21、34......

    求第30位数是多少, 用递归算法实现。(C#语言)

答案: public int  test(int n)
        {
            int m = 0;
            if (n == 1 || n == 2)
                return 1;
            else
            {
                m = test(n - 1) + test(n - 2);
                return m;
            }
        }

2、

 public class A
    {
        public virtual void Fun1(int i)
        {
           
            Console.WriteLine(i);
        }

        public void Fun2(A a)
        {
            a.Fun1(1);
            Fun1(5);
        }
    }


    public class B : A
    {
        public override void Fun1(int i)
        {
            base.Fun1(i + 1);
        }

     
    }

求下面输出的是什么

B b = new B();
            A a = new A();
            a.Fun2(b);
            b.Fun2(a);
答案:2516

 

3、

public abstract class A
    {
        public A()
        {
           HttpContext.Current.Response.Write('A');
        }
        public virtual void Fun()
        {
         
           HttpContext.Current.Response.Write("A.Fun()");
        }
    }

    public class B : A
    {
        public B()
        {
           HttpContext.Current.Response.Write('B');
        }

        public new void Fun()
        {
           HttpContext.Current.Response.Write("B.Fun()");
        }

     
    }

求下面输出的是什么?

A a = new B();
 a.Fun();

答案:ABA.Fun()

 

解析:.net处理这种问题有两种机制:1,子类使用new关键字屏蔽父类的方法,则调用谁的方法由“定义时的类型决定”;2,子类使用override关键字重写父类方法,则调用谁的方法由”运行时引用真实的对象决定“。

A a = new B();
你定义了一个父类变量,却引用了之类的真实对象,那么a.Fun()怎么执行呢?由于你程序中之类使用new机制,所以调用谁的方法由“定义时的类型决定”,所以是调用父类的方法。

 

4、  程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)

要求:  1.要有联动性,老鼠和主人的行为是被动的。

2.考虑可扩展性,猫的叫声可能引起其他联动效应。

答案

 

public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Cat2 cat = new Cat2();
            mouse2 mou = new mouse2("mous1", cat);
            Master2 master = new Master2(cat);
         
            cat.Cry();

        }
    }
   public interface Observer2{
       void Response();
   }
    public interface Subject2
    {
        void AimAt(Observer2 obs); 
    }
    public class mouse2: Observer2
    {
        private string name;

        public mouse2(string name, Subject2 subj)
        {
            this.name = name;
            subj.AimAt(this);
        }
        public void Response()
        {
            HttpContext.Current.Response.Write(name + " attempt to escape!");
        }
    }
    public class Master2 : Observer2
    {
        public Master2(Subject2 subj)
        {
   
            subj.AimAt(this);
        }
        public void Response()
        {
            HttpContext.Current.Response.Write("Host waken");
        }
    }
    public class Cat2:Subject2
    {
        private ArrayList observers;

        public Cat2()
        {
            this.observers = new ArrayList();

        }
        public void Cry()
        {
            HttpContext.Current.Response.Write("Cat cryed!");
             foreach (Observer2 obs in this.observers)
             {
                obs.Response();
            }

        }
        public void AimAt(Observer2 asb)
        {
            observers.Add(asb);
        }
    }

 

posted @ 2011-10-16 12:45  ※繁星※  阅读(734)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3