面试题(面向对象程序设计)

以下内容转载自C#与.net面试宝典

1.对象

1.1 类和结构有什么区别?定义和使用非常相似,例子如下:

public struct Student
{
    string Name;
    int Age;
}


public class Question
{
    int Number;
     string Content;
}


使用:

Student s=new Student();
Question q=new Question();

解答: 虽然类和结构都能使用new关键字,但他们的差别较大,主要体现在3个方面:结构是值类型,类是引用类型;结构不能被继承,而类能被继承,结构和类的内部结构不同。

 

1.2 简述C#的虚方法

使用virtual修饰的方法是虚方法,virtual关键字用来修饰属性,方法,事件等。并使它们能够被子类重写,虚方法必须在父类中实现,它的作用是可以再派生类中重写。

 

1.3 简述C#中的密封类和密封方法

sealed class Person:IFormattable
{

      public string name { get; set; }

}

class Student:Person
 {

      //Student继承Person类编译阶段会报错

}

 

//父类Person

class Person:IFormattable
 {

        //定义虚方法

        public virtual void print()
        {
            Console.WriteLine("父类的print方法");
        }

}

//子类Student

class Student:Person
 {

        //子类重写虚方法,并将用sealed修饰

        public sealed override void print()
        {
            Console.WriteLine("子类的print方法");
        }

}

//Student的子类MiddleStudent

class MiddleStudent : Student
{
        //由于Student将print方法密封,使其无法重写
}

密封类使用sealed关键字进行修饰,它不能作为其他类的基类,并且它没有派生类。密封类的作用是防止其他类继承该类。密封方法使用sealed关键字进行修饰,它不影响类的继承,但被他修饰的虚方法不能被重写。

 

1.4 请介绍C#中静态类构造方法的特点

 

class Student:Person
{
        static Student()
        {
            Console.WriteLine("静态构造方法");
        }

        public Student()
        {
            Console.WriteLine("构造方法");

        }

}

 

解答:静态构造方法有四个特点,最主要的特点是:静态构造方法是最先被执行的构造方法,并且在一个类中只允许有一个无参的静态构造方法。

 

1.5 简述C#派生类中的构造函数

使用C#派生类中的构造函数时,需要注意关键字base和this的区别,关键字base表示父类中的构造函数,关键字this表示子类中的构造函数。

 

1.6 简述接口及接口继承

接口是把属性和方法隐式的封装起来,以封装特定功能的一个集合。当定义了接口,就必须在继承类中实现它,这样类就支持接口的属性和方法。

 

2.事件

2.1 列举一个事件和委托的实例


Zhao.cs:

    public class Zhao
    {
        public Zhao()
        {
            Console.WriteLine("生成小赵");
        }

        public void DeductMoney(object sender,EventArgs e)
        {
            Console.WriteLine("小赵:好小子,上班时间胆敢玩游戏...");
            Console.WriteLine("小赵:你不知道上班打游戏要罚款的吗?");
            Li li = (Li)sender;
            Console.WriteLine("小李的工资:" + li.Money);
            Console.WriteLine("开始扣钱...");
            System.Threading.Thread.Sleep(500);
            li.Money = li.Money - 400;
            Console.WriteLine("钱扣完了,还剩下:" + li.Money);
        }

    }


    Li.cs:

    public class Li
    {
        public event PlayingGameHandler playGame;
        public decimal Money { get; set; }

        public Li()
        {
            this.Money = 2000;
            Console.WriteLine("生肖小李");
        }
        
        public void Game()
        {
            Console.WriteLine("小李开始玩游戏了");
            Console.WriteLine("小李:魔兽好好玩,哈哈");
            System.Threading.Thread.Sleep(500);
            System.EventArgs args = new EventArgs();
            if(playGame != null)
            {
                playGame(this, args);
            }
        }

        protected virtual void OnPlayingGame(EventArgs e)
        {
            if(playGame != null)
            {
                playGame(this, e);
            }
        }
    }

program.cs:

    [STAThread]
    static void Main(string[] args)
    {
            Console.WriteLine("场景开始了...");
            Zhao zhao = new Zhao();
            Li li = new Li();
            li.playGame += new PlayingGameHandler(zhao.DeductMoney);//指定监视
            li.Game();
            Console.WriteLine("场景结束...");
            Console.Read();
    }

解析:委托有如下几个要素:

1.激发事件的对象:小李

2.处理对象事件的对象:小赵

3.定义委托:就是你让小赵监视小李

 

3.委托

3.1 什么是委托?

        //定义一个委托
        public delegate double ChengFun(double num);

        static double sqrt(double num)
        {
             return num * num;
        }

        static void Main(string[] args)
        {
            ChengFun cheng = new ChengFun(sqrt);
            double result = cheng(5);
            Console.WriteLine(result);
            Console.Read();
        }

 

解答:C#的委托类都继承自,System.Delegate类型。委托类型的声明与方法签名有些类似,有一个返回值和任意数量,任意类型的参数。委托是一种可以封装命名和匿名方法的引用类型。委托有点类似于指正,但委托是安全可靠的。

 

3.2 C#中被委托的方法必须是静态的吗?

委托不仅能绑定静态方法,也能绑定实例方法。当绑定实例方法时,delegate.Target属性指向该实例方法所属类型的一个实例对象,当绑定静态方法时delegate.Target属性会变设置为null。

 

想获取更多.NET面试题,提高面试成功率,请参考:《.NET面试宝典》

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值