欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

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

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

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

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

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

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

定義抽象屬性

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

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

若要編譯該示例,請(qǐng)使用以下命令:

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

聲明抽象屬性時(shí)(如本示例中的 Area),指明哪些屬性訪問(wèn)器可用即可,不要實(shí)現(xiàn)它們。在此示例中,只有一個(gè) get 訪問(wèn)器可用,因此該屬性是只讀的。
下面的代碼演示 Shape 的三個(gè)子類,并演示它們?nèi)绾沃貙?Area 屬性來(lái)提供自己的實(shí)現(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;
    }
  }
}

下面的代碼演示一個(gè)測(cè)試程序,它創(chuàng)建若干 Shape 派生對(duì)象,并輸出它們的面積。

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

上一篇:講解C#面相對(duì)象編程中的類與對(duì)象的特性與概念

欄    目:C#教程

下一篇:C#編程中枚舉類型的使用教程

本文標(biāo)題:深入解析C#中的abstract抽象類

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6734.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有