C#學(xué)習(xí)基礎(chǔ)概念二十五問(wèn)第3/4頁(yè)
更新時(shí)間:2007年04月09日 00:00:00 作者:
4.abstract 是什么意思?
答:
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訪問(wèn)器表示繼承類必須將該屬性實(shí)現(xiàn)為可讀寫(xiě)
public abstract String Attribute
{
get;
set;
}
//抽象方法,傳入一個(gè)字符串參數(shù)無(wú)返回值
public abstract void Function(String value);
//抽象事件,類型為系統(tǒng)預(yù)定義的代理(delegate):EventHandler
public abstract event EventHandler Event;
//抽象索引指示器,只具有g(shù)et訪問(wèn)器表示繼承類必須將該索引指示器實(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();
}
}
}
結(jié)果:
1234567
7
6
5
4
3
2
1
相關(guān)文章
UnityShader3實(shí)現(xiàn)轉(zhuǎn)圈與冷卻效果
這篇文章主要為大家詳細(xì)介紹了UnityShader3實(shí)現(xiàn)轉(zhuǎn)圈與冷卻效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03Unity3D實(shí)現(xiàn)控制攝像機(jī)移動(dòng)
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)控制攝像機(jī)移動(dòng) ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02C#使用JavaScriptSerializer序列化時(shí)的時(shí)間類型處理
這篇文章主要為大家詳細(xì)介紹了C#使用JavaScriptSerializer序列化時(shí)的時(shí)間類型處理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08C#實(shí)現(xiàn).net頁(yè)面之間傳值傳參方法匯總
這篇文章主要介紹了C#實(shí)現(xiàn).net頁(yè)面之間傳值傳參方法,實(shí)例匯總了幾類常見(jiàn)的傳值傳參的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10C#開(kāi)發(fā)windows服務(wù)實(shí)現(xiàn)自動(dòng)從FTP服務(wù)器下載文件
這篇文章主要為大家詳細(xì)介紹了C#開(kāi)發(fā)windows服務(wù)實(shí)現(xiàn)自動(dòng)從FTP服務(wù)器下載文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03WindowsForm實(shí)現(xiàn)警告消息框的實(shí)例代碼
這篇文章主要介紹了WindowsForm如何實(shí)現(xiàn)警告消息框,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07