繼承(加上封裝和多形性)是面向對象的編程的三個主要特性(也稱爲“支柱”)之一。繼承用於創建可重用、擴展和修改在其他類中定義的行爲的新類。其成員被繼承的類稱爲“基類”,繼承這些成員的類稱爲“派生類”。派生類只能有一個直接基類。但是,繼承是可傳遞的。如果 ClassB 派生出 ClassC,ClassA 派生出 ClassB,則 ClassC 會繼承 ClassB 和 ClassA 中聲明的成員。
從概念上來說,派生類是基類的專用化。 例如,如果您有一個基類 Animal,則可以有一個名爲Mammal 的派生類和一個名爲Reptile 的派生類。Mammal 是一個Animal,Reptile 也是一個Animal,但每個派生類均表示基類的不同專用化。
基類與派生類型理解:
如:A(1、2、3、4、5、6、7、8、9)//基類
B(1、2、3、4、5)//派生類1
C(5、6、7、8、9)//派生類2
D(6、7、8、9、0、11、12、13)//派生類3
B、C、D都具有 A 的特徵。
繼承的傳遞:(還是以上面的來講)
B(1、2、3、4、5)//本是 A 的派生類;卻爲 E 的基類
E(1、3、5)// B 的派生類
因爲 E 爲 B 的派生類 ,B 爲 A 的派生類,所以 E 也爲 A 的派生類
定義一個類從其他類派生時,派生類隱式獲得基類的所有成員 ( 除 構造函數 和 析構函數 外)。因此,派生類可以重用基類中的代碼而無需重新實現這些代碼。可以在派生類中添加更多成員。派生類以這種方式擴展基類的功能。
下圖演示一個 WorkItem 類,該類表示某業務流程中的一個工作項。和所有的類一樣,該類派生自System.Object 並繼承其所有方法。WorkItem 添加了自己的五個成員。其中包括一個構造函數,因爲構造函數不能繼承。類ChangeRequest 繼承自WorkItem 並表示特定種類的工作項。ChangeRequest 在它從WorkItem 和Object 繼承的成員中另外添加了兩個成員。它必須添加其自己的構造函數,還添加originalItemID。利用屬性originalItemID,可將ChangeRequest 實例與更改請求將應用到的原始WorkItem 相關聯。
下面的示例演示如何以 C# 表示上圖所示的類關係。 該示例還演示 WorkItem 如何重寫虛方法Object.ToString,以及ChangeRequest 類如何繼承該方法的WorkItem 實現。
// 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();
}
}
/* Output:
1 - Fix Bugs
2 - Change the Design of the Base Class
*/