C#中abstract的用法详解

abstract可以用来修饰类,方法,属性,索引器和时间,这里不包括字段. 使用abstrac修饰的类,该类只能作为其他类的基类,不能实例化,而且abstract修饰的成员在派生类中必须全部实现,不允许部分实现,否则编译异常. 如:

using System;
namespace ConsoleApplication8 
{ 
  class Program 
  { 
    static void Main(string[] args) 
    { 
      BClass b = new BClass(); 
      b.m1(); 
    } 
  } 
  abstract class AClass 
  { 
      public abstract void m1(); 
      public abstract void m2(); 
  } 
  class BClass : AClass 
  { 
    public override void m1() 
    { 
      throw new NotImplementedException(); 
    } 
    //public override void m2() 
    //{ 
      // throw new NotImplementedException(); 
    //} 
  } 
}

 Abstract classes have the following features:

抽象类拥有如下特征:

1,抽象类不能被实例化, 但可以有实例构造函数, 类是否可以实例化取决于是否拥有实例化的权限 (对于抽象类的权限是abstract, 禁止实例化),
即使不提供构造函数, 编译器也会提供默认构造函数;

2,抽象类可以包含抽象方法和访问器;

3,抽象类不能使用sealed修饰, sealed意为不能被继承;

4,所有继承自抽象类的非抽象类必须实现所有的抽象成员,包括方法,属性,索引器,事件;

abstract修饰的方法有如下特征:

1,抽象方法即是虚拟方法(隐含);

2,抽象方法只能在抽象类中声明;

3,因为抽象方法只是声明, 不提供实现, 所以方法只以分号结束,没有方法体,即没有花括号部分;如

public abstract void MyMethod();

4,override修饰的覆盖方法提供实现,且只能作为非抽象类的成员;

5,在抽象方法的声明上不能使用virtual或者是static修饰.即不能是静态的,又因为abstract已经是虚拟的,无需再用virtual强调.
抽象属性尽管在行为上与抽象方法相似,但仍有有如下不同:

1,不能在静态属性上应用abstract修饰符;

2,抽象属性在非抽象的派生类中覆盖重写,使用override修饰符;

抽象类与接口:

1,抽象类必须提供所有接口成员的实现;

2,继承接口的抽象类可以将接口的成员映射位抽象方法.

interface I 
{ 
void M(); 
} 
abstract class C: I 
{ 
public abstract void M(); 
} 
抽象类实例: 
// abstract_keyword.cs 
// 抽象类 
using System; 
abstract class BaseClass // 抽象类 
{ 
protected int _x = 100; //抽象类可以定义字段,但不可以是抽象字段,也没有这一说法. 
protected int _y = 150; 
public BaseClass(int i) //可以定义实例构造函数,仅供派生的非抽象类调用; 这里显式提供构造函数,编译器将不再提供默认构造函数. 
{ 
fielda = i; 
} 
public BaseClass() 
{ 
} 
private int fielda; 
public static int fieldsa = 0; 
public abstract void AbstractMethod(); // 抽象方法 
public abstract int X { get; } //抽象属性 
public abstract int Y { get; } 
public abstract string IdxString { get; set; } //抽象属性 
public abstract char this[int i] { get; } //抽象索引器 
}
class DerivedClass : BaseClass 
{ 
private string idxstring; 
private int fieldb; 
//如果基类中没有定义无参构造函数,但存在有参数的构造函数, 
//那么这里派生类得构造函数必须调用基类的有参数构造函数,否则编译出错 
public DerivedClass(int p) 
: base(p) //这里的:base(p)可省略,因为基类定义了默认的无参构造函数 
{ 
fieldb = p; 
} 
public override string IdxString //覆盖重新属性 
{ 
get
{ 
return idxstring; 
} 
set
{ 
idxstring = value; 
} 
} 
public override char this[int i] //覆盖重写索引器 
{ 
get { return IdxString[i]; } 
} 
public override void AbstractMethod() 
{ 
_x++; 
_y++; 
}
public override int X // 覆盖重写属性
{
 get
 {
  return _x + 10;
 }
}
 
public override int Y // 覆盖重写属性
{
 get
 {
  return _y + 10;
 }
}
 
static void Main()
{
 DerivedClass o = new DerivedClass(1);
 o.AbstractMethod();
 Console.WriteLine("x = {0}, y = {1}", o.X, o.Y);
}
}
原文地址:https://www.cnblogs.com/BluceLee/p/13846690.html