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

詳解C#面相對象編程中的繼承特性

 更新時間:2016年01月28日 17:19:19   投稿:goldensun  
這篇文章主要介紹了C#面相對象編程中的繼承特性,是C#入門學習中的基礎知識,需要的朋友可以參考下

繼承(加上封裝和多態(tài)性)是面向對象的編程的三個主要特性(也稱為“支柱”)之一。 繼承用于創(chuàng)建可重用、擴展和修改在其他類中定義的行為的新類。其成員被繼承的類稱為“基類”,繼承這些成員的類稱為“派生類”。派生類只能有一個直接基類。但是,繼承是可傳遞的。如果 ClassB 派生出 ClassC,ClassA 派生出 ClassB,則 ClassC 會繼承 ClassB 和 ClassA 中聲明的成員。

注意
結構不支持繼承,但可以實現(xiàn)接口。

從概念上來說,派生類是基類的特例。 例如,如果您有一個基類 Animal,則可以有一個名為 Mammal 的派生類和一個名為 Reptile 的派生類。 Mammal 是一個 Animal,Reptile 也是一個 Animal,但每個派生類均表示基類的不同專用化。
定義一個類從其他類派生時,派生類隱式獲得基類的除構造函數(shù)和析構函數(shù)以外的所有成員。因此,派生類可以重用基類中的代碼而無需重新實現(xiàn)這些代碼??梢栽谂缮愔刑砑痈喑蓡T。派生類以這種方式擴展基類的功能。
下圖演示一個 WorkItem 類,該類表示某業(yè)務流程中的一個工作項。和所有的類一樣,該類派生自 System.Object 并繼承其所有方法。 WorkItem 添加了自己的五個成員。其中包括一個構造函數(shù),因為構造函數(shù)不能繼承。類ChangeRequest 繼承自 WorkItem 并表示特定種類的工作項。 ChangeRequest 在它從 WorkItem 和 Object 繼承的成員中另外添加了兩個成員。它必須添加其自己的構造函數(shù),還添加 originalItemID。利用屬性 originalItemID,可將 ChangeRequest 實例與更改請求將應用到的原始 WorkItem 相關聯(lián)。

2016128171832088.jpeg (449×302)

類繼承
下面的示例演示如何以 C# 表示上圖所示的類關系。該示例還演示 WorkItem 如何重寫虛方法 Object.ToString,以及 ChangeRequest 類如何繼承該方法的 WorkItem 實現(xiàn)。

// WorkItem implicitly inherits from the Object class.
public class WorkItem
{
  // Static field currentID stores the job ID of the last WorkItem that
  // has been created.
  private static int currentID;

  //Properties.
  protected int ID { get; set; }
  protected string Title { get; set; }
  protected string Description { get; set; }
  protected TimeSpan jobLength { get; set; }

  // Default constructor. If a derived class does not invoke a base-
  // class constructor explicitly, the default constructor is called
  // implicitly. 
  public WorkItem()
  {
    ID = 0;
    Title = "Default title";
    Description = "Default description.";
    jobLength = new TimeSpan();
  }

  // Instance constructor that has three parameters.
  public WorkItem(string title, string desc, TimeSpan joblen)
  {
    this.ID = GetNextID();
    this.Title = title;
    this.Description = desc;
    this.jobLength = joblen;
  }

  // Static constructor to initialize the static member, currentID. This
  // constructor is called one time, automatically, before any instance
  // of WorkItem or ChangeRequest is created, or currentID is referenced.
  static WorkItem()
  {
    currentID = 0;
  }


  protected int GetNextID()
  {
    // currentID is a static field. It is incremented each time a new
    // instance of WorkItem is created.
    return ++currentID;
  }

  // Method Update enables you to update the title and job length of an
  // existing WorkItem object.
  public void Update(string title, TimeSpan joblen)
  {
    this.Title = title;
    this.jobLength = joblen;
  }

  // Virtual method override of the ToString method that is inherited
  // from System.Object.
  public override string ToString()
  {
    return String.Format("{0} - {1}", this.ID, this.Title);
  }
}

// ChangeRequest derives from WorkItem and adds a property (originalItemID) 
// and two constructors.
public class ChangeRequest : WorkItem
{
  protected int originalItemID { get; set; }

  // Constructors. Because neither constructor calls a base-class 
  // constructor explicitly, the default constructor in the base class
  // is called implicitly. The base class must contain a default 
  // constructor.

  // Default constructor for the derived class.
  public ChangeRequest() { }

  // Instance constructor that has four parameters.
  public ChangeRequest(string title, string desc, TimeSpan jobLen,
             int originalID)
  {
    // The following properties and the GetNexID method are inherited 
    // from WorkItem.
    this.ID = GetNextID();
    this.Title = title;
    this.Description = desc;
    this.jobLength = jobLen;

    // Property originalItemId is a member of ChangeRequest, but not 
    // of WorkItem.
    this.originalItemID = originalID;
  }
}

class Program
{
  static void Main()
  {
    // Create an instance of WorkItem by using the constructor in the 
    // base class that takes three arguments.
    WorkItem item = new WorkItem("Fix Bugs",
                   "Fix all bugs in my code branch",
                   new TimeSpan(3, 4, 0, 0));

    // Create an instance of ChangeRequest by using the constructor in
    // the derived class that takes four arguments.
    ChangeRequest change = new ChangeRequest("Change Base Class Design",
                         "Add members to the class",
                         new TimeSpan(4, 0, 0),
                         1);

    // Use the ToString method defined in WorkItem.
    Console.WriteLine(item.ToString());

    // Use the inherited Update method to change the title of the 
    // ChangeRequest object.
    change.Update("Change the Design of the Base Class",
      new TimeSpan(4, 0, 0));

    // ChangeRequest inherits WorkItem's override of ToString.
    Console.WriteLine(change.ToString());

    // Keep the console open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
  }
}

輸出:

  1 - Fix Bugs
  2 - Change the Design of the Base Class

相關文章

  • c#多線程編程基礎

    c#多線程編程基礎

    線程是程序中一個單一的順序控制流程.在單個程序中同時運行多個線程完成不同的工作,稱為多線程,本文使用示例介紹一下多線程的使用方法
    2014-02-02
  • c#中oracle的to_date函數(shù)使用方法

    c#中oracle的to_date函數(shù)使用方法

    C#使用參數(shù)傳值方式操作oracle的date字段,主要介紹了oracle的to_date使用方法,大家參考使用吧
    2014-01-01
  • C# 泛型集合的自定義類型排序的實現(xiàn)

    C# 泛型集合的自定義類型排序的實現(xiàn)

    這篇文章主要介紹了C# 泛型集合的自定義類型排序的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • C# FileSystemWatcher 在監(jiān)控文件夾和文件時的使用方法

    C# FileSystemWatcher 在監(jiān)控文件夾和文件時的使用方法

    這篇文章主要介紹了C# FileSystemWatcher 在監(jiān)控文件夾和文件時的使用方法,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以參考下
    2020-06-06
  • C#算法設計與分析詳解

    C#算法設計與分析詳解

    本文詳細講解了C#的算法設計與分析,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • c# 用Base64實現(xiàn)文件上傳

    c# 用Base64實現(xiàn)文件上傳

    這篇文章主要介紹了c# 用Base64實現(xiàn)文件上傳的方法,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-08-08
  • C#中的multipart/form-data提交文件和參數(shù)

    C#中的multipart/form-data提交文件和參數(shù)

    這篇文章主要介紹了C#中的multipart/form-data提交文件和參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 完成OSS.Http底層HttpClient重構封裝 支持標準庫

    完成OSS.Http底層HttpClient重構封裝 支持標準庫

    OSS.Http項目對于.Net Standard標準庫的支持已經(jīng)遷移完畢,OSS開源系列兩個最底層的類庫已經(jīng)具備跨運行時支持的能力。本篇文章主要包含 1. HttpClient的介紹,2. 重構的思路, 3. 容易遇到的問題。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • C#多線程之任務的用法詳解

    C#多線程之任務的用法詳解

    本文詳細講解了C#多線程之任務的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C# 反射(Reflection)的用處分析

    C# 反射(Reflection)的用處分析

    反射(Reflection)是C#里很重要的一個特性,其它語言也有這個特性,比如JAVA。反射這個特性是很實用的,如果使用過struts, hibernate, spring等等這些框架的話,便會知道反射這個特性是多么的強大了。在我接觸過的那些框架中,沒有一個框架是不使用反射的。
    2015-03-03

最新評論