自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C# abstract修飾符淺析

開發(fā) 后端
這里介紹C# abstract修飾符可以用于類、方法、屬性、事件和索引指示器(indexer),表示其為抽象成員 abstract 不可以和 static 、virtual 一起使用聲明為 abstract 成員可以不包括實現(xiàn)代碼。

C#語言有很多值得學習的地方,這里我們主要介紹C# abstract修飾符,包括介紹通常用于強制繼承類必須實現(xiàn)某一成員。等方面。

C# abstract修飾符是什么意思?

C# abstract修飾符可以用于類、方法、屬性、事件和索引指示器(indexer),表示其為抽象成員 abstract 不可以和 static 、virtual 一起使用聲明為 abstract 成員可以不包括實現(xiàn)代碼,但只要類中還有未實現(xiàn)的抽象成員(即抽象類),那么它的對象就不能被實例化,通常用于強制繼承類必須實現(xiàn)某一成員。

示例:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.    
  5. namespace Example04  
  6. {  
  7. #region 基類,抽象類  
  8. public abstract class BaseClass  
  9. {  
  10. //抽象屬性,同時具有g(shù)et和set訪問器表示繼承類必須將該屬性實現(xiàn)為可讀寫  
  11. public abstract String Attribute  
  12. {  
  13. get;  
  14. set;  
  15. }  
  16.    
  17. //抽象方法,傳入一個字符串參數(shù)無返回值  
  18. public abstract void Function(String value);  
  19.    
  20. //抽象事件,類型為系統(tǒng)預(yù)定義的代理(delegate):EventHandler  
  21. public abstract event EventHandler Event;  
  22.    
  23. //抽象索引指示器,只具有g(shù)et訪問器表示繼承類必須將該索引指示器實現(xiàn)為只讀  
  24. public abstract Char this[int Index]  
  25. {  
  26. get;  
  27. }  
  28. }  
  29. #endregion  
  30.    
  31. #region 繼承類  
  32. public class DeriveClass : BaseClass  
  33. {  
  34. private String attribute;  
  35.    
  36. public override String Attribute  
  37. {  
  38. get  
  39. {  
  40. return attribute;  
  41. }  
  42. set  
  43. {  
  44. attribute = value;  
  45. }  
  46. }  
  47. public override void Function(String value)  
  48. {  
  49. attribute = value;  
  50. if (Event != null)  
  51. {  
  52. Event(this, new EventArgs());  
  53. }  
  54. }  
  55. public override event EventHandler Event;  
  56. public override Char this[int Index]  
  57. {  
  58. get  
  59. {  
  60. return attribute[Index];  
  61. }  
  62. }  
  63. }  
  64. #endregion  
  65.    
  66. class Program  
  67. {  
  68. static void OnFunction(object sender, EventArgs e)  
  69. {  
  70. for (int i = 0; i < ((DeriveClass)sender).Attribute.Length; i++)  
  71. {  
  72. Console.WriteLine(((DeriveClass)sender)[i]);  
  73. }  
  74. }  
  75. static void Main(string[] args)  
  76. {  
  77. DeriveClass tmpObj = new DeriveClass();  
  78.    
  79. tmpObj.Attribute = "1234567";  
  80. Console.WriteLine(tmpObj.Attribute);  
  81.    
  82. //將靜態(tài)函數(shù)OnFunction與tmpObj對象的Event事件進行關(guān)聯(lián)  
  83. tmpObj.Event += new EventHandler(OnFunction);  
  84.    
  85. tmpObj.Function("7654321");  
  86.    
  87. Console.ReadLine();  
  88. }  
  89. }  

【編輯推薦】

  1. C# const常量詳細介紹
  2. C# Lambda表達式學習筆記
  3. C#隱式類型局部變量經(jīng)驗總結(jié)
  4. 調(diào)用C# Thread.Start()方法
  5. C# CheckStatus()方法
責任編輯:佚名 來源: 博客園
相關(guān)推薦

2009-08-27 13:06:13

C# new修飾符

2009-08-24 16:49:39

C#修飾符

2009-09-02 17:14:28

C#修飾符

2009-08-27 11:04:08

C# extern修飾

2009-08-21 13:58:06

C# virtual修

2009-08-27 11:16:40

C# sealed修飾

2009-09-04 11:06:40

C#訪問修飾符

2009-09-02 17:04:35

C# Extern修飾

2011-06-28 09:29:11

C#修飾符

2009-08-12 17:03:39

C# Static修飾

2024-09-27 09:50:11

C#正則表達式

2009-08-12 09:30:10

C#??運算符

2023-12-29 09:01:27

SwiftUI視圖修飾符

2011-07-20 16:50:39

inlinec++

2011-07-20 16:48:22

C++static

2011-07-20 16:57:05

C++const

2015-08-18 09:25:11

Java修飾符關(guān)鍵詞

2009-08-12 10:27:12

C#運算符重載運算符重載實例

2009-08-19 17:20:22

C# 操作符

2010-01-11 18:46:15

VB.NET修飾符
點贊
收藏

51CTO技術(shù)棧公眾號