詳解C#面相對(duì)象編程中的繼承特性
繼承(加上封裝和多態(tài)性)是面向?qū)ο蟮木幊痰娜齻€(gè)主要特性(也稱為“支柱”)之一。 繼承用于創(chuàng)建可重用、擴(kuò)展和修改在其他類中定義的行為的新類。其成員被繼承的類稱為“基類”,繼承這些成員的類稱為“派生類”。派生類只能有一個(gè)直接基類。但是,繼承是可傳遞的。如果 ClassB 派生出 ClassC,ClassA 派生出 ClassB,則 ClassC 會(huì)繼承 ClassB 和 ClassA 中聲明的成員。
注意
結(jié)構(gòu)不支持繼承,但可以實(shí)現(xiàn)接口。
從概念上來說,派生類是基類的特例。 例如,如果您有一個(gè)基類 Animal,則可以有一個(gè)名為 Mammal 的派生類和一個(gè)名為 Reptile 的派生類。 Mammal 是一個(gè) Animal,Reptile 也是一個(gè) Animal,但每個(gè)派生類均表示基類的不同專用化。
定義一個(gè)類從其他類派生時(shí),派生類隱式獲得基類的除構(gòu)造函數(shù)和析構(gòu)函數(shù)以外的所有成員。因此,派生類可以重用基類中的代碼而無需重新實(shí)現(xiàn)這些代碼??梢栽谂缮愔刑砑痈喑蓡T。派生類以這種方式擴(kuò)展基類的功能。
下圖演示一個(gè) WorkItem 類,該類表示某業(yè)務(wù)流程中的一個(gè)工作項(xiàng)。和所有的類一樣,該類派生自 System.Object 并繼承其所有方法。 WorkItem 添加了自己的五個(gè)成員。其中包括一個(gè)構(gòu)造函數(shù),因?yàn)闃?gòu)造函數(shù)不能繼承。類ChangeRequest 繼承自 WorkItem 并表示特定種類的工作項(xiàng)。 ChangeRequest 在它從 WorkItem 和 Object 繼承的成員中另外添加了兩個(gè)成員。它必須添加其自己的構(gòu)造函數(shù),還添加 originalItemID。利用屬性 originalItemID,可將 ChangeRequest 實(shí)例與更改請(qǐng)求將應(yīng)用到的原始 WorkItem 相關(guān)聯(lián)。
類繼承
下面的示例演示如何以 C# 表示上圖所示的類關(guān)系。該示例還演示 WorkItem 如何重寫虛方法 Object.ToString,以及 ChangeRequest 類如何繼承該方法的 WorkItem 實(shí)現(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#中自動(dòng)實(shí)現(xiàn)的屬性
欄 目:C#教程
下一篇:在C#的類或結(jié)構(gòu)中重寫ToString方法的用法簡介
本文標(biāo)題:詳解C#面相對(duì)象編程中的繼承特性
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6744.html
您可能感興趣的文章
- 01-10C#中Socket通信用法實(shí)例詳解
- 01-10C#實(shí)現(xiàn)獲取不同對(duì)象中名稱相同屬性的方法
- 01-10C#裝箱和拆箱原理詳解
- 01-10C#編程自學(xué)之類和對(duì)象
- 01-10C#類的多態(tài)性詳解
- 01-10C#創(chuàng)建不規(guī)則窗體的4種方式詳解
- 01-10C#中深度復(fù)制和淺度復(fù)制詳解
- 01-10C#編程實(shí)現(xiàn)對(duì)象與JSON串互相轉(zhuǎn)換實(shí)例分析
- 01-10C#數(shù)據(jù)結(jié)構(gòu)之隊(duì)列(Quene)實(shí)例詳解
- 01-10C#數(shù)據(jù)結(jié)構(gòu)之順序表(SeqList)實(shí)例詳解


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


