新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
C#抽象類(lèi)簡(jiǎn)單概述
本文向大家介紹C#抽象類(lèi),可能好多人還不了解C#抽象類(lèi),沒(méi)有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。

C#抽象類(lèi)將一個(gè)(或多個(gè))方法或?qū)傩月暶鳛槌橄蟮摹_@樣的方法并不具有聲明它們的類(lèi)中提供的實(shí)現(xiàn),盡管C#抽象類(lèi)也可以包含非抽象方法,也就是說(shuō),已經(jīng)為其方法提供了實(shí)現(xiàn)。C#抽象類(lèi)不能直接實(shí)例化,而只能作為派生類(lèi)。這樣的派生類(lèi)必須為所有的抽象方法和屬性提供實(shí)現(xiàn)(使用 override 關(guān)鍵字),除非派生成員本身被聲明為抽象的。
下面的示例聲明了一個(gè)抽象的 Employee 類(lèi)。我們還創(chuàng)建了一個(gè)名為 Manager 的派生類(lèi),它提供了定義在 Employee 類(lèi)中的抽象方法 show() 的實(shí)現(xiàn):
- using System;
- public abstract class Employee
- {
- // abstract show method
- public abstract void show();
- }
- // Manager class extends Employee
- public class Manager: Employee
- {
- string name;
- public Manager(string name)
- {
- this.name = name;
- }
- //override the show method
- public override void show()
- {
- Console.WriteLine("Name : " + name);
- }
- }
- public class CreateManager
- {
- public static void Main(string[] args)
- {
- // Create instance of Manager and assign it to an Employee reference
- Employee temp = new Manager("John Chapman");
- //Call show method. This will call the show method of the Manager class
- temp.show();
- }
- }
這段代碼調(diào)用了由 Manager 類(lèi)提供的 show() 實(shí)現(xiàn),并且在屏幕上打印出雇員的名字。以上介紹C#抽象類(lèi)
【編輯推薦】
- C#方法重寫(xiě)全面介紹
- C#流程控制語(yǔ)句簡(jiǎn)單描述
- Java和C#字符串類(lèi)型概述
- C#訪問(wèn)修飾符詳細(xì)剖析
- 選擇C#構(gòu)造函數(shù)描述
當(dāng)前標(biāo)題:C#抽象類(lèi)簡(jiǎn)單概述
轉(zhuǎn)載來(lái)于:http://www.5511xx.com/article/cdgssdp.html


咨詢
建站咨詢
