十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
C#語言有很多值得學(xué)習(xí)的地方,這里我們主要介紹C# abstract修飾符,包括介紹通常用于強(qiáng)制繼承類必須實(shí)現(xiàn)某一成員。等方面。

成都創(chuàng)新互聯(lián)是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設(shè)公司,自成立以來公司不斷探索創(chuàng)新,始終堅(jiān)持為客戶提供滿意周到的服務(wù),在本地打下了良好的口碑,在過去的10年時(shí)間我們累計(jì)服務(wù)了上千家以及全國政企客戶,如成都人造霧等企業(yè)單位,完善的項(xiàng)目管理流程,嚴(yán)格把控項(xiàng)目進(jìn)度與質(zhì)量監(jiān)控加上過硬的技術(shù)實(shí)力獲得客戶的一致稱揚(yáng)。
C# abstract修飾符是什么意思?
C# abstract修飾符可以用于類、方法、屬性、事件和索引指示器(indexer),表示其為抽象成員 abstract 不可以和 static 、virtual 一起使用聲明為 abstract 成員可以不包括實(shí)現(xiàn)代碼,但只要類中還有未實(shí)現(xiàn)的抽象成員(即抽象類),那么它的對(duì)象就不能被實(shí)例化,通常用于強(qiáng)制繼承類必須實(shí)現(xiàn)某一成員。
示例:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Example04
- {
- #region 基類,抽象類
- public abstract class BaseClass
- {
- //抽象屬性,同時(shí)具有g(shù)et和set訪問器表示繼承類必須將該屬性實(shí)現(xiàn)為可讀寫
- public abstract String Attribute
- {
- get;
- set;
- }
- //抽象方法,傳入一個(gè)字符串參數(shù)無返回值
- public abstract void Function(String value);
- //抽象事件,類型為系統(tǒng)預(yù)定義的代理(delegate):EventHandler
- public abstract event EventHandler Event;
- //抽象索引指示器,只具有g(shù)et訪問器表示繼承類必須將該索引指示器實(shí)現(xiàn)為只讀
- public abstract Char this[int Index]
- {
- get;
- }
- }
- #endregion
- #region 繼承類
- public class DeriveClass : BaseClass
- {
- private String attribute;
- public override String Attribute
- {
- get
- {
- return attribute;
- }
- set
- {
- attribute = value;
- }
- }
- public override void Function(String value)
- {
- attribute = value;
- if (Event != null)
- {
- Event(this, new EventArgs());
- }
- }
- public override event EventHandler Event;
- public override Char this[int Index]
- {
- get
- {
- return attribute[Index];
- }
- }
- }
- #endregion
- class Program
- {
- static void OnFunction(object sender, EventArgs e)
- {
- for (int i = 0; i < ((DeriveClass)sender).Attribute.Length; i++)
- {
- Console.WriteLine(((DeriveClass)sender)[i]);
- }
- }
- static void Main(string[] args)
- {
- DeriveClass tmpObj = new DeriveClass();
- tmpObj.Attribute = "1234567";
- Console.WriteLine(tmpObj.Attribute);
- //將靜態(tài)函數(shù)OnFunction與tmpObj對(duì)象的Event事件進(jìn)行關(guān)聯(lián)
- tmpObj.Event += new EventHandler(OnFunction);
- tmpObj.Function("7654321");
- Console.ReadLine();
- }
- }
- }