欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年01月29日 15:10:36   投稿:goldensun  
這篇文章主要介紹了深入解析C#中的abstract抽象類,包括定義抽象類等C#面相對象編程中的基礎(chǔ)知識,需要的朋友可以參考下

抽象類和類成員
通過在類定義前面放置關(guān)鍵字 abstract,可以將類聲明為抽象類。例如:

public abstract class A
{
  // Class members here.
}

抽象類不能實例化。抽象類的用途是提供一個可供多個派生類共享的通用基類定義。例如,類庫可以定義一個抽象類,將其用作多個類庫函數(shù)的參數(shù),并要求使用該庫的程序員通過創(chuàng)建派生類來提供自己的類實現(xiàn)。
抽象類也可以定義抽象方法。方法是將關(guān)鍵字 abstract 添加到方法的返回類型的前面。例如:

public abstract class A
{
  public abstract void DoWork(int i);
}

抽象方法沒有實現(xiàn),所以方法定義后面是分號,而不是常規(guī)的方法塊。抽象類的派生類必須實現(xiàn)所有抽象方法。當(dāng)抽象類從基類繼承虛方法時,抽象類可以使用抽象方法重寫該虛方法。例如:

// 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,則該方法對于從抽象類繼承的所有類而言仍然是虛方法。繼承一個抽象方法的類不能訪問該方法的原始實現(xiàn)。在上一個示例中,類 F 中的 DoWork 不能調(diào)用類 D 中的 DoWork。通過這種方式,抽象類可以強(qiáng)制派生類為虛方法提供新的方法實現(xiàn)。

定義抽象屬性

下面的示例演示如何定義抽象屬性。抽象屬性聲明不提供屬性訪問器的實現(xiàn),它只聲明該類支持屬性,而將訪問器實現(xiàn)留給派生類。下面的示例演示如何實現(xiàn)從基類繼承的抽象屬性。
此示例由三個文件組成,其中每個文件都單獨(dú)編譯,產(chǎn)生的程序集由下一次編譯引用:

  • abstractshape.cs:包含抽象 Area 屬性的 Shape 類。
  • shapes.cs:Shape 類的子類。
  • shapetest.cs:測試程序,它顯示某些 Shape 派生對象的面積。

若要編譯該示例,請使用以下命令:

csc abstractshape.cs shapes.cs shapetest.cs

這樣將生成可執(zhí)行文件 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),指明哪些屬性訪問器可用即可,不要實現(xiàn)它們。在此示例中,只有一個 get 訪問器可用,因此該屬性是只讀的。
下面的代碼演示 Shape 的三個子類,并演示它們?nèi)绾沃貙?Area 屬性來提供自己的實現(xiàn)。

// 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;
    }
  }
}

下面的代碼演示一個測試程序,它創(chuàng)建若干 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

相關(guān)文章

最新評論