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

C#抽象類將一個(gè)(或多個(gè))方法或?qū)傩月暶鳛槌橄蟮摹_@樣的方法并不具有聲明它們的類中提供的實(shí)現(xiàn),盡管C#抽象類也可以包含非抽象方法,也就是說,已經(jīng)為其方法提供了實(shí)現(xiàn)。C#抽象類不能直接實(shí)例化,而只能作為派生類。這樣的派生類必須為所有的抽象方法和屬性提供實(shí)現(xiàn)(使用 override 關(guān)鍵字),除非派生成員本身被聲明為抽象的。
下面的示例聲明了一個(gè)抽象的 Employee 類。我們還創(chuàng)建了一個(gè)名為 Manager 的派生類,它提供了定義在 Employee 類中的抽象方法 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 類提供的 show() 實(shí)現(xiàn),并且在屏幕上打印出雇員的名字。以上介紹C#抽象類
【編輯推薦】
- C#方法重寫全面介紹
- C#流程控制語句簡(jiǎn)單描述
- Java和C#字符串類型概述
- C#訪問修飾符詳細(xì)剖析
- 選擇C#構(gòu)造函數(shù)描述
新聞名稱:C#抽象類簡(jiǎn)單概述
本文來源:http://www.dlmjj.cn/article/cdgssdp.html


咨詢
建站咨詢
