ASP.NET Prepared for Interview (1)

来源:互联网 发布:和人工智能有关的股票 编辑:程序博客网 时间:2024/05/17 03:11


Ques: 1 What does the keyword virtual declare for a method?

If a method use virtual as the keyword, that means it's derived class( a class inherit it) can override this method.

class MyBaseClass{    // virtual auto-implemented property. Overrides can only    // provide specialized behavior if they implement get and set accessors.    public virtual string Name { get; set; }    // ordinary virtual property with backing field    private int num;    public virtual int Number    {        get { return num; }        set { num = value; }    }}class MyDerivedClass : MyBaseClass //inherit the class MyBaseClass{    private string name;   // Override auto-implemented property with ordinary property   // to provide specialized accessor behavior.    public override string Name    {        get        {            return name;        }        set        {            if (value != String.Empty)            {                name = value;            }            else            {                name = "Unknown";            }        }    }}



Ques: 2 What is the difference between an interface and abstract class ?

(1) what is interface

the classes can implement a interface, if that it need implement all of the member from interface

interface ISampleInterface{    void SampleMethod();}class ImplementationClass : ISampleInterface  // class xxx to implement interface xxx{    // Explicit interface member implementation:     void ISampleInterface.SampleMethod() // implement member of interface    {        // Method implementation.    }    static void Main()    {        // Declare an interface instance.        ISampleInterface obj = new ImplementationClass();        // Call the member.        obj.SampleMethod();    }}



(2) what is abstract  class

An abstract class cannot be instantiated(实例化)

abstract class ShapesClass{    abstract public int Area();}class Square : ShapesClass{    int side = 0;    public Square(int n)    {        side = n;    }    // Area method is required to avoid    // a compile-time error.    public override int Area()    {        return side * side;    }    static void Main()     {        Square sq = new Square(12);        Console.WriteLine("Area of the square = {0}", sq.Area());    }    interface I    {        void M();    }    abstract class C : I    {        public abstract void M();    }}// Output: Area of the square = 144

抽象类的作用是实现了多态性  (polymorphism) 

--- 几个子类都继承了一个抽象类, 他们可以对一个抽象方法进行重写。  这样 其他无关类 在实例化不同的子类时  就可以调用实现不同的方法


例子: 动物类是抽象类,叫是抽象方法。 不同的动物子类可以重写成不同的叫方法, 狗可以重写为 返回 wangwang, 猫可以重写为 返回 miaomiao。

 using System;  abstract class BaseClass   // Abstract class    {        protected int _x = 100;        protected int _y = 150;        public abstract void AbstractMethod();   // Abstract method        public abstract int X    { get; }        public abstract int Y    { get; }    }    class DerivedClass : BaseClass    {        public override void AbstractMethod()        {            _x++;            _y++;        }        public override int X   // overriding property        {            get            {                return _x + 10;            }        }        public override int Y   // overriding property        {            get            {                return _y + 10;            }        }    } class DerivedClass1 : BaseClass    {        public override void AbstractMethod()        {            _x++;            _y++;        }        public override int X   // overriding property        {            get            {                return _x + 100;            }        }        public override int Y   // overriding property        {            get            {                return _y + 100;            }        }    }public class test{   static void Main()        {            DerivedClass o = new DerivedClass();            o.AbstractMethod();            Console.WriteLine("x = {0}, y = {1}", o.X, o.Y);DerivedClass1 o1 = new DerivedClass1();            o1.AbstractMethod();            Console.WriteLine("x = {0}, y = {1}", o1.X, o1.Y);        }}


抽象类的重写只能用于 继承关系, 即 只能子类重写父类中的抽象方法

而接口可以被任何类进行实现

c#中 接口还可以 弥补 无法进行多继承的缺点, 因为c#支持接口的多实现(implement)

..................................................................................

1. In an interface class, all methods are abstract and there is no implementation.  In an abstract class some methods can be concrete.2. In an interface class, no accessibility modifiers are allowed.  An abstract class may have accessibility modifiers.

.....................................................................................................................................................................................................

Access modifiers are keywords used to specify the declared accessibility of a member or a type. This section introduces the four access modifiers:

  • public
  • protected
  • internal
  • private

Following are the five levels of access modifiers :-
√ Private : Only members of class have access.
√ Protected :-All members in current class and in derived classes can access the
variables.
√ Friend (internal in C#) :- Only members in current project have access to the
elements.
√ Protected friend (protected internal in C#) :- All members in current project
and all members in derived class can access the variables.
√ Public :- All members have access in all classes and projects.

....................................................................................................................................................................................................

What is ArrayList ?
Array is whose size can increase and decrease dynamically. Array list can hold item of
different types. As Array list can increase and decrease size dynamically you do not have
to use the REDIM keyword. You can access any item in array using the INDEX value of
the array position.

.....................................................................................................................................................................................................


一、抽象类:
      抽象类是特殊的类,只是不能被实例化;除此以外,具有类的其他特性;重要的是抽象类可以包括抽象方法,这是普通类所不能的。抽象方法只能声明于抽象类中,且不包含任何实现,派生类必须覆盖它们。另外,抽象类可以派生自一个抽象类,可以覆盖基类的抽象方法也可以不覆盖,如果不覆盖,则其派生类必须覆盖它们。

       二、接口:
      接口是引用类型的,类似于类,和抽象类的相似之处有三点:
       1、不能实例化;
       2、包含未实现的方法声明;
       3、派生类必须实现未实现的方法,抽象类是抽象方法,接口则是所有成员(不仅是方法包括其他成员);

       另外,接口有如下特性:
接口除了可以包含方法之外,还可以包含属性、索引器、事件,而且这些成员都被定义为公有的。除此之外,不能包含任何其他的成员,例如:常量、域、构造函数、析构函数、静态成员。一个类可以直接继承多个接口,但只能直接继承一个类(包括抽象类)。


      三、抽象类和接口的区别:
      1.类是对对象的抽象,可以把抽象类理解为把类当作对象,抽象成的类叫做抽象类.而接口只是一个行为的规范或规定,微软的自定义接口总是后带able字段,证明其是表述一类类“我能做。。。”.抽象类更多的是定义在一系列紧密相关的类间,而接口大多数是关系疏松但都实现某一功能的类中. 
      2.接口基本上不具备继承的任何具体特点,它仅仅承诺了能够调用的方法;     
      3.一个类一次可以实现若干个接口,但是只能扩展一个父类     
      4.接口可以用于支持回调,而继承并不具备这个特点.     
      5.抽象类不能被密封。   
      6.抽象类实现的具体方法默认为虚的,但实现接口的类中的接口方法却默认为非虚的,当然您也可以声明为虚的. 
      7.(接口)与非抽象类类似,抽象类也必须为在该类的基类列表中列出的接口的所有成员提供它自己的实现。但是,允许抽象类将接口方法映射到抽象方法上。   
      8.抽象类实现了oop中的一个原则,把可变的与不可变的分离。抽象类和接口就是定义为不可变的,而把可变的座位子类去实现。   
      9.好的接口定义应该是具有专一功能性的,而不是多功能的,否则造成接口污染。如果一个类只是实现了这个接口的中一个功能,而不得不去实现接口中的其他方法,就叫接口污染。   
     10.尽量避免使用继承来实现组建功能,而是使用黑箱复用,即对象组合。因为继承的层次增多,造成最直接的后果就是当你调用这个类群中某一类,就必须把他们全部加载到栈中!后果可想而知.(结合堆栈原理理解)。同时,有心的朋友可以留意到微软在构建一个类时,很多时候用到了对象组合的方法。比如asp.net中,Page类,有Server Request等属性,但其实他们都是某个类的对象。使用Page类的这个对象来调用另外的类的方法和属性,这个是非常基本的一个设计原则。   
     11.如果抽象类实现接口,则可以把接口中方法映射到抽象类中作为抽象方法而不必实现,而在抽象类的子类中实现接口中方法.
   
      四、抽象类和接口的使用:
      1. 如果预计要创建组件的多个版本,则创建抽象类。抽象类提供简单的方法来控制组件版本。
      2.如果创建的功能将在大范围的全异对象间使用,则使用接口。如果要设计小而简练的功能块,则使用接口。
      3.如果要设计大的功能单元,则使用抽象类.如果要在组件的所有实现间提供通用的已实现功能,则使用抽象类。   
      4.抽象类主要用于关系密切的对象;而接口适合为不相关的类提供通用功能。

 一、抽象类:
      抽象类是特殊的类,只是不能被实例化;除此以外,具有类的其他特性;重要的是抽象类可以包括抽象方法,这是普通类所不能的。抽象方法只能声明于抽象类中,且不包含任何实现,派生类必须覆盖它们。另外,抽象类可以派生自一个抽象类,可以覆盖基类的抽象方法也可以不覆盖,如果不覆盖,则其派生类必须覆盖它们。

       二、接口:
      接口是引用类型的,类似于类,和抽象类的相似之处有三点:
       1、不能实例化;
       2、包含未实现的方法声明;
       3、派生类必须实现未实现的方法,抽象类是抽象方法,接口则是所有成员(不仅是方法包括其他成员);

       另外,接口有如下特性:
接口除了可以包含方法之外,还可以包含属性、索引器、事件,而且这些成员都被定义为公有的。除此之外,不能包含任何其他的成员,例如:常量、域、构造函数、析构函数、静态成员。一个类可以直接继承多个接口,但只能直接继承一个类(包括抽象类)。


      三、抽象类和接口的区别:
      1.类是对对象的抽象,可以把抽象类理解为把类当作对象,抽象成的类叫做抽象类.而接口只是一个行为的规范或规定,微软的自定义接口总是后带able字段,证明其是表述一类类“我能做。。。”.抽象类更多的是定义在一系列紧密相关的类间,而接口大多数是关系疏松但都实现某一功能的类中. 
      2.接口基本上不具备继承的任何具体特点,它仅仅承诺了能够调用的方法;     
      3.一个类一次可以实现若干个接口,但是只能扩展一个父类     
      4.接口可以用于支持回调,而继承并不具备这个特点.     
      5.抽象类不能被密封。   
      6.抽象类实现的具体方法默认为虚的,但实现接口的类中的接口方法却默认为非虚的,当然您也可以声明为虚的. 
      7.(接口)与非抽象类类似,抽象类也必须为在该类的基类列表中列出的接口的所有成员提供它自己的实现。但是,允许抽象类将接口方法映射到抽象方法上。   
      8.抽象类实现了oop中的一个原则,把可变的与不可变的分离。抽象类和接口就是定义为不可变的,而把可变的座位子类去实现。   
      9.好的接口定义应该是具有专一功能性的,而不是多功能的,否则造成接口污染。如果一个类只是实现了这个接口的中一个功能,而不得不去实现接口中的其他方法,就叫接口污染。   
     10.尽量避免使用继承来实现组建功能,而是使用黑箱复用,即对象组合。因为继承的层次增多,造成最直接的后果就是当你调用这个类群中某一类,就必须把他们全部加载到栈中!后果可想而知.(结合堆栈原理理解)。同时,有心的朋友可以留意到微软在构建一个类时,很多时候用到了对象组合的方法。比如asp.net中,Page类,有Server Request等属性,但其实他们都是某个类的对象。使用Page类的这个对象来调用另外的类的方法和属性,这个是非常基本的一个设计原则。   
     11.如果抽象类实现接口,则可以把接口中方法映射到抽象类中作为抽象方法而不必实现,而在抽象类的子类中实现接口中方法.
   
      四、抽象类和接口的使用:
      1. 如果预计要创建组件的多个版本,则创建抽象类。抽象类提供简单的方法来控制组件版本。
      2.如果创建的功能将在大范围的全异对象间使用,则使用接口。如果要设计小而简练的功能块,则使用接口。
      3.如果要设计大的功能单元,则使用抽象类.如果要在组件的所有实现间提供通用的已实现功能,则使用抽象类。   
      4.抽象类主要用于关系密切的对象;而接口适合为不相关的类提供通用功能。


接口是为了实现 程序的构建可插入性 弥补C#中无法多继承的不足    和支持多语言编程(C# 和 VB etc)

抽象类是为了实现 程序的多态性(polymorphism)


 以下是我在网上看到的几个形象比喻,真的非常不错,呵呵:
1.飞机会飞,鸟会飞,他们都继承了同一个接口“飞”;但是F22属于飞机抽象类,鸽子属于鸟抽象类。

2. 就像铁门木门都是门(抽象类),你想要个门我给不了(不能实例化),但我可以给你个具体的铁门或木门(多态);而且只能是门,你不能说它是窗(单继承);一个门可以有锁(接口)也可以有门铃(多实现)。 门(抽象类)定义了你是什么,接口(锁)规定了你能做什么(一个接口最好只能做一件事,你不能要求锁也能发出声音吧(接口污染))。


interface: the method in the interface don't have body

interface IBooks{long int Cost{get;}}public class ComputerBooks : IBooks{public long int Cost{get {return 100;}}}

Different: 

  1. A class may inherit only one abstract class, but may implement more interface
  2. Abstract class can have many access modifiers, but interface only have one -- public
  3. The method in Abstract class may or may not have an implementation, But in Interface, method only have definition, no implementation


...........................................................................................................................................................................................................................................................................................................

方法的重载(overload)和覆盖(override)

 

有的时候,类的同一种功能有多种实现方式,到底采用哪种实现方式,取决于调用者给定的参数。例如我们最常用的System.out.println()能够打印出任何数据类型的数据,它有多种实现方式。运行时,Java虚拟机先判断给定参数的类型,然后决定执行哪个println()方法。

 

重载(overload):对于类的方法(包括从父类中继承的方法),方法名相同参数列表不同的方法之间就构成了重载关系。这里有两个问题需要注意:

(1)       什么叫参数列表?参数列表又叫参数签名,指三样东西:参数的类型参数的个数参数的顺序。这三者只要有一个不同就叫做参数列表不同。

(2)       重载关系只能发生在同一个类中吗?非也。这时候你要深刻理解继承,要知道一个子类所拥有的成员除了自己显式写出来的以外,还有父类遗传下来的。所以子类中的某个方法和父类中继承下来的方法也可以发生重载的关系。

大家在使用的时候要紧扣定义,看方法之间是否是重载关系,不用管方法的修饰符和返回类型以及抛出的异常,只看方法名和参数列表。而且要记住,构造器也可以重载。

 

覆盖 (override):也叫重写,就是在当父类中的某些方法不能满足要求时,子类中改写父类的方法。当父类中的方法被覆盖了后,除非用super关键字,否则就无法再调用父类中的方法了。

发生覆盖的条件:

1、“三同一不低” 子类和父类的方法名称参数列表返回类型必须完全相同,而且子类方法的访问修饰符的权限不能比父类

2、子类方法不能抛出比父类方法更多的异常。即子类方法所抛出的异常必须和父类方法所抛出的异常一致,或者是其子类,或者什么也不抛出

3、被覆盖的方法不能是final类型的。因为final修饰的方法是无法覆盖的。

4、被覆盖的方法不能为private。否则在其子类中只是新定义了一个方法,并没有对其进行覆盖。

5、被覆盖的方法不能为static。所以如果父类中的方法为静态的,而子类中的方法不是静态的,但是两个方法除了这一点外其他都满足覆盖条件,那么会发生编译错误。反之亦然。即使父类和子类中的方法都是静态的,并且满足覆盖条件,但是仍然不会发生覆盖,因为静态方法是在编译的时候把静态方法和类的引用类型进行匹配。

 

方法的覆盖和重载具有以下相同点:

都要求方法同名

都可以用于抽象方法和非抽象方法之间

 

方法的覆盖和重载具有以下不同点:

方法覆盖要求参数列表(参数签名)必须一致,而方法重载要求参数列表必须不一致。

方法覆盖要求返回类型必须一致,方法重载对此没有要求。

方法覆盖只能用于子类覆盖父类的方法,方法重载用于同一个类中的所有方法(包括从父类中继承而来的方法)

方法覆盖对方法的访问权限和抛出的异常有特殊的要求,而方法重载在这方面没有任何限制。

父类的一个方法只能被子类覆盖一次,而一个方法可以在所有的类中可以被重载多次。


......................................................................................................................................................................................................................................................................................................

  • ++i will increment the value of i, and then return the incremented value.

     i = 1; j = ++i; (i is 2, j is 2)
  • i++ will increment the value of i, but return the pre-incremented value.

     i = 1; j = i++; (i is 2, j is 1)
.............................................................................................................................................................................................................................................................................................................


int d; d = Convert.Tolnt32( !(30 < 20) );
result:

A value 1 will be assigned to d.

.....................................................................................................................................................................................................................................

 the correct size of a Decimal datatype is16 Bytes.......................................................................................................................................................................................................................................

short s1 = 20;short s2 = 400;int a;a = s1 * s2;
output is: A value 8000 will be assigned to a..............................................................................................................................................................................................................................................

The Reference in C#

ref : 传递前必须初始化,
out:不必初始化,就算初始化,也会被无视,
out 作为输出参数,可以解决函数只有一个返回值的问题,
class SampleProgram    {        static void Main(string[] args)        {             int num = 1;            funcv(num);             Console.Write(num + ", ");             funcr(ref num);             Console.Write(num + ", ");        }        static void funcv(int num)        {             num = num + 10; Console.Write(num + ", ");        }        static void funcr (ref int num)        {             num = num + 10; Console.Write(num + ", ");        }     } 
The Output is:
11, 1, 11, 11,.........................................................................................................................................................

The Enumeration usage in C#
using System;namespace stds{public enum states{morning = 0,afternoon = 1,evening = 2}class test{   static void displayer(states s)  {    // Console.WriteLine(s);       if(s == states.morning)  {    Console.WriteLine("good morning!");  }  else if(s == states.afternoon)  {     Console.WriteLine("good afternoon!");  }  else if(s == states.evening)  {     Console.WriteLine("good evening!");  }  }    static void Main()  {     states s = states.morning;     displayer(s);      Console.WriteLine("******************");  states s1 = states.afternoon; Console.WriteLine(s1.ToString()); // output "afternoon" Console.WriteLine(Convert.ToInt32(s1)); // output 1    }}}






















原创粉丝点击