C#基礎(chǔ)語法:Base關(guān)鍵字學(xué)習(xí)筆記
它與this關(guān)鍵字一樣,都是作為類的實(shí)例(因此不能調(diào)用基類的靜態(tài)成員和抽象成員)簡寫或者替代而存在的,只不過this關(guān)鍵字用于替代本類的實(shí)例,base關(guān)鍵字用于替代基類的實(shí)例,用法很簡單,其訪問基類的形式如下:
base.【標(biāo)識(shí)符】
base[【表達(dá)式列表】] 這個(gè)類型的一看便可以大概猜測多用于基類實(shí)例的索引器操作,在我下面演示的代碼中你會(huì)看到它的用法。
對于 base.【標(biāo)識(shí)符】的訪問形式再次說明一下:
對于非虛方法,這種訪問僅僅是對基類實(shí)例成員的直接訪問,完全等價(jià)于((base)this).【標(biāo)識(shí)符】。
對于虛方法,對于這種訪子類重寫該虛方法運(yùn)用這種訪問形式也是(禁用了虛方法調(diào)用的機(jī)制)對基類實(shí)例成員的直接訪問,將其看做非虛方法處理,此時(shí)則不等價(jià)于((base)this).【標(biāo)識(shí)符】,因?yàn)檫@種格式完全遵守虛方法調(diào)用的機(jī)制,其聲明試時(shí)為積累類型,運(yùn)行時(shí)為子類類型,所以執(zhí)行的還是子類的重寫方法。于未重寫的虛方法等同于簡單的非虛方法處理。
測試代碼如下:
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是對基類構(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)文章
Unity OnGUI實(shí)時(shí)顯示游戲FPS
這篇文章主要為大家詳細(xì)介紹了Unity OnGUI實(shí)時(shí)顯示游戲FPS,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
基于Unity3D實(shí)現(xiàn)3D迷宮小游戲的示例代碼
迷宮游戲作為經(jīng)典的小游戲,一直深受大家的喜愛。本文小編將為大家詳細(xì)介紹一下如何用Unity實(shí)現(xiàn)一個(gè)3D版的迷宮小游戲,感興趣的可以動(dòng)手試一試2022-03-03
datatable生成excel和excel插入圖片示例詳解
excel導(dǎo)出在C#代碼中應(yīng)用己經(jīng)很廣泛了,下面講了datatable生成excel、復(fù)制sheet頁、刪除sheet頁、選中sheet頁、另存excel文件、excel中插入圖片等功能2014-01-01
基于不要返回null之EmptyFactory的應(yīng)用詳解
本篇文章對不要返回null之EmptyFactory進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
c#調(diào)用api控制windows關(guān)機(jī)示例(可以重啟/注銷)
本文介紹了c#控制windows關(guān)機(jī)、重啟、注銷的二種方法,分為調(diào)用windows自帶的shutdown.exe關(guān)機(jī)和調(diào)用API關(guān)機(jī)的方法2014-01-01
Winform+.Net6實(shí)現(xiàn)圖片拖拽上傳功能
這篇文章主要為大家詳細(xì)介紹了如何使用WinformPictureBox控件+.Net6 WebApi實(shí)現(xiàn)圖片拖拽上傳功能,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2023-09-09

