C#基礎(chǔ)語法:Base關(guān)鍵字學(xué)習(xí)筆記
它與this關(guān)鍵字一樣,都是作為類的實(shí)例(因此不能調(diào)用基類的靜態(tài)成員和抽象成員)簡(jiǎn)寫或者替代而存在的,只不過this關(guān)鍵字用于替代本類的實(shí)例,base關(guān)鍵字用于替代基類的實(shí)例,用法很簡(jiǎn)單,其訪問基類的形式如下:
base.【標(biāo)識(shí)符】
base[【表達(dá)式列表】] 這個(gè)類型的一看便可以大概猜測(cè)多用于基類實(shí)例的索引器操作,在我下面演示的代碼中你會(huì)看到它的用法。
對(duì)于 base.【標(biāo)識(shí)符】的訪問形式再次說明一下:
對(duì)于非虛方法,這種訪問僅僅是對(duì)基類實(shí)例成員的直接訪問,完全等價(jià)于((base)this).【標(biāo)識(shí)符】。
對(duì)于虛方法,對(duì)于這種訪子類重寫該虛方法運(yùn)用這種訪問形式也是(禁用了虛方法調(diào)用的機(jī)制)對(duì)基類實(shí)例成員的直接訪問,將其看做非虛方法處理,此時(shí)則不等價(jià)于((base)this).【標(biāo)識(shí)符】,因?yàn)檫@種格式完全遵守虛方法調(diào)用的機(jī)制,其聲明試時(shí)為積累類型,運(yùn)行時(shí)為子類類型,所以執(zhí)行的還是子類的重寫方法。于未重寫的虛方法等同于簡(jiǎn)單的非虛方法處理。
測(cè)試代碼如下:
using System; namespace BaseTest { class father { string str1 = "this field[1] of baseclass", str2 = "this field[2] of baseclass"; public void F1() //Non-virtual method { Console.WriteLine(" F1 of the baseclass"); } public virtual void F2()//virtual method { Console.WriteLine(" F2 of the baseclass"); } public virtual void F3() { Console.WriteLine(" F3 of the baseclass that is not overrided "); } public string this[int index] { set { if (index==1 ) { str1 = value; } else { str2 = value; } } get { if (index ==1) { return str1; } else { return str2; } } } } class Child:father { public void G() { Console.WriteLine("======Non-virtual methods Test ========="); base.F1(); ((father)this).F1(); Console.WriteLine("======virtual methods Test========="); base.F2(); ((father)this).F2(); base.F3(); ((father)this).F3(); Console.WriteLine("=====Test the type that the tbase [[expression]] =========="); Console.WriteLine(base[1]); base[1] = "override the default "; Console.WriteLine(base[1]); Console.WriteLine("================Test Over====================="); } public override void F2() { Console.WriteLine(" F2 of the subclass "); } static void Main(string[] args) { Child child=new Child(); child.G(); Console.ReadKey(); } } }
base用于構(gòu)造函數(shù)聲明,用法和this用于構(gòu)造函數(shù)聲明完全一致,但base是對(duì)基類構(gòu)造函數(shù)形參的匹配。
using System; namespace BaseCoTest { class Base { public Base(int a, string str) { Console.WriteLine("Base. Base(int a,string str)"); } public Base(int a) { Console.WriteLine("Base. Base(int a)"); } public Base() { } } class Sub : Base { public Sub() { } public Sub(int a) : base(1, "123") { Console.WriteLine("Sub .Sub(int a)"); } class Test { public static void Main() { Sub sub = new Sub(1); Console.ReadKey(); } } } }
相關(guān)文章
解決C# winForm自定義鼠標(biāo)樣式的兩種實(shí)現(xiàn)方法詳解
本篇文章是對(duì)在C#中winForm自定義鼠標(biāo)樣式的兩種實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05分享WCF聊天程序--WCFChat實(shí)現(xiàn)代碼
無意中在一個(gè)國(guó)外的站點(diǎn)下到了一個(gè)利用WCF實(shí)現(xiàn)聊天的程序,作者是:Nikola Paljetak。研究了一下,自己做了測(cè)試和部分修改,感覺還不錯(cuò),分享給大家2015-11-11C# winform 請(qǐng)求http的實(shí)現(xiàn)(get,post)
本文主要介紹了C# winform 請(qǐng)求http的實(shí)現(xiàn)(get,post),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Unity實(shí)現(xiàn)ScrollView滑動(dòng)吸附功能
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)ScrollView滑動(dòng)吸附功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09