当前位置: 移动技术网 > IT编程>开发语言>c# > 深入解析C#中的abstract抽象类

深入解析C#中的abstract抽象类

2019年07月18日  | 移动技术网IT编程  | 我要评论

抽象类和类成员
通过在类定义前面放置关键字 abstract,可以将类声明为抽象类。例如:

public abstract class a
{
  // class members here.
}

抽象类不能实例化。抽象类的用途是提供一个可供多个派生类共享的通用基类定义。例如,类库可以定义一个抽象类,将其用作多个类库函数的参数,并要求使用该库的程序员通过创建派生类来提供自己的类实现。
抽象类也可以定义抽象方法。方法是将关键字 abstract 添加到方法的返回类型的前面。例如:

public abstract class a
{
  public abstract void dowork(int i);
}

抽象方法没有实现,所以方法定义后面是分号,而不是常规的方法块。抽象类的派生类必须实现所有抽象方法。当抽象类从基类继承虚方法时,抽象类可以使用抽象方法重写该虚方法。例如:

// compile with: /target:library
public class d
{
  public virtual void dowork(int i)
  {
    // original implementation.
  }
}

public abstract class e : d
{
  public abstract override void dowork(int i);
}

public class f : e
{
  public override void dowork(int i)
  {
    // new implementation.
  }
}

如果将 virtual 方法声明为 abstract,则该方法对于从抽象类继承的所有类而言仍然是虚方法。继承一个抽象方法的类不能访问该方法的原始实现。在上一个示例中,类 f 中的 dowork 不能调用类 d 中的 dowork。通过这种方式,抽象类可以强制派生类为虚方法提供新的方法实现。

定义抽象属性

下面的示例演示如何定义抽象属性。抽象属性声明不提供属性访问器的实现,它只声明该类支持属性,而将访问器实现留给派生类。下面的示例演示如何实现从基类继承的抽象属性。
此示例由三个文件组成,其中每个文件都单独编译,产生的程序集由下一次编译引用:

  • abstractshape.cs:包含抽象 area 属性的 shape 类。
  • shapes.cs:shape 类的子类。
  • shapetest.cs:测试程序,它显示某些 shape 派生对象的面积。

若要编译该示例,请使用以下命令:

csc abstractshape.cs shapes.cs shapetest.cs

这样将生成可执行文件 shapetest.exe。
该文件声明的 shape 类包含 double 类型的 area 属性。

// compile with: csc /target:library abstractshape.cs
public abstract class shape
{
  private string name;

  public shape(string s)
  {
    // calling the set accessor of the id property.
    id = s;
  }

  public string id
  {
    get
    {
      return name;
    }

    set
    {
      name = value;
    }
  }

  // area is a read-only property - only a get accessor is needed:
  public abstract double area
  {
    get;
  }

  public override string tostring()
  {
    return id + " area = " + string.format("{0:f2}", area);
  }
}

属性的修饰符就放置在属性声明中。例如:

public abstract double area

声明抽象属性时(如本示例中的 area),指明哪些属性访问器可用即可,不要实现它们。在此示例中,只有一个 get 访问器可用,因此该属性是只读的。
下面的代码演示 shape 的三个子类,并演示它们如何重写 area 属性来提供自己的实现。

// compile with: csc /target:library /reference:abstractshape.dll shapes.cs
public class square : shape
{
  private int side;

  public square(int side, string id)
    : base(id)
  {
    this.side = side;
  }

  public override double area
  {
    get
    {
      // given the side, return the area of a square:
      return side * side;
    }
  }
}

public class circle : shape
{
  private int radius;

  public circle(int radius, string id)
    : base(id)
  {
    this.radius = radius;
  }

  public override double area
  {
    get
    {
      // given the radius, return the area of a circle:
      return radius * radius * system.math.pi;
    }
  }
}

public class rectangle : shape
{
  private int width;
  private int height;

  public rectangle(int width, int height, string id)
    : base(id)
  {
    this.width = width;
    this.height = height;
  }

  public override double area
  {
    get
    {
      // given the width and height, return the area of a rectangle:
      return width * height;
    }
  }
}

下面的代码演示一个测试程序,它创建若干 shape 派生对象,并输出它们的面积。

// compile with: csc /reference:abstractshape.dll;shapes.dll shapetest.cs
class testclass
{
  static void main()
  {
    shape[] shapes =
    {
      new square(5, "square #1"),
      new circle(3, "circle #1"),
      new rectangle( 4, 5, "rectangle #1")
    };

    system.console.writeline("shapes collection");
    foreach (shape s in shapes)
    {
      system.console.writeline(s);
    }
  }
}

输出:

  shapes collection
  square #1 area = 25.00
  circle #1 area = 28.27
  rectangle #1 area = 20.00

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网