一,继承

  1,无论基类的可访问性如何,除构造函数和析构函数以外,其他所有成员都能够被继承。基类的private成员在子类中不可访问。

    如果将子类的对象作为参数传入基类,就可以访问基类的private成员。

  2,类不能循环继承,否则会出错。

  3,方法的隐藏

  

代码
public class A
{
public A()
{ }
protected static string name = "xiao";
protected static void F()
{
Console.Write(
"A.F()\n");
}
}
public class B : A
{
public B()
{ }
private static string name = "chun";
new
protected static void F() //deliberately hide
{
Console.Write(
"B.F()\n");
}
protected static void G()
{
F();
}
}
public class C : B
{
public C()
{ }
public static void D()
{
Console.Write(
"name={0}\n", name);//xiao
F();//B.F()
}
}

 

 

 

二,多态

  1,如果一个实例化方法中有virtual修饰,则称为虚方法。可以使用override进行重写。虚方法和非虚方法的区别:

 

代码
public class A
{
public A()
{ }
public void S()
{
Console.Write(
"A.S()\n");
}
public virtual void F()
{
Console.Write(
"A.F()\n");
}
}
public class B : A
{
public B()
{ }
public void S()
{
Console.Write(
"B.S()\n");
}
public override void F()
{
Console.Write(
"B.F()\n");
}
}

static void Main(string[] args)
{
A a
= new A();
B b
= new B();
A c
= b;
a.S();
a.F();
b.S();
b.F();
c.S();
//调用A.S();
c.F();//调用B.F();多态的体现
Console.Read();
}

  3,除了虚方法和重写外,C#还支持虚属性,索引器和事件以及重写它们。

三,关键字base

  1,base只可以用于实例化方法,实例化构造函数,事例访问器中。

  

代码
public class A
{
public A()
{
Console.Write(
"A()\n");
}
public void S()
{
Console.Write(
"A.S()\n");
}

}
public class B : A
{
public B():base()
{
Console.Write(
"B()\n");
}
public void S()
{

Console.Write(
"B.S()\n");
}
public void F()
{
base.S();//等价于((A)this).S()
Console.Write("B.F()\n");
}
}

static void Main(string[] args)
{
B b
= new B();
b.F();
Console.Read();
}

四,抽象类

  1,抽象类只能定义抽象成员。

  2,使用abstract修饰抽象类和抽象成员,用override重写抽象方法。

五,密封类

  1,使用sealed修饰,是为了防止继承的滥用。

  2,也可以使用sealed修饰方法。则该方法不能被重写。

六,静态类

  1,使用static修饰,只能从object继承。只能包含静态成员,常量和嵌套类型被看成是静态成员。

  2,默认访问修饰限制符为public。不能够为其他修饰符。

七,接口

  1,接口的成员只能是方法,属性,事件,索引器。不能是常量,字段,运算符,事例构造函数,析构函数或者类型。

  2,接口只能包含声明(签名),不能包含实现。

  3,接口的成员是public修饰,但是不能使用public。实际上接口不能使用除new以外的其他修饰符。

  4,接口可以继承一个或多个接口。接口不能从类继承。