深入解析C#中的abstract抽象類
抽象類和類成員
通過(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
您可能感興趣的文章
- 01-10深入淺出23種設(shè)計(jì)模式
- 01-10C#中查找Dictionary中的重復(fù)值的方法
- 01-10C#將圖片存放到SQL SERVER數(shù)據(jù)庫(kù)中的方法
- 01-10關(guān)于nancy中的身份驗(yàn)證
- 01-10C#中的事務(wù)用法實(shí)例分析
- 01-10C#實(shí)現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法
- 01-10C#中的正則表達(dá)式介紹
- 01-10C#開發(fā)中的垃圾回收機(jī)制簡(jiǎn)析
- 01-10C#語(yǔ)言中的修飾符匯總
- 01-10C#中的 == 和equals()區(qū)別淺析


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 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#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法