C#類中方法的執(zhí)行順序是什么
有些中級(jí)開發(fā)小伙伴還是搞不太明白在繼承父類以及不同場(chǎng)景實(shí)例化的情況下,父類和子類的各種方法的執(zhí)行順序到底是什么,下面通過場(chǎng)景的舉例來重新認(rèn)識(shí)下方法的執(zhí)行順序:
(下面內(nèi)容涉及到了C#中的繼承,構(gòu)造函數(shù),虛方法,虛方法的重寫,new關(guān)鍵字等知識(shí)點(diǎn))
場(chǎng)景一
有子類繼承,但是只實(shí)例化父類:只執(zhí)行A對(duì)象,輸出A對(duì)象的信息
class A
{
public A() => Console.WriteLine("A的構(gòu)造函數(shù)");
public virtual void Fun() => Console.WriteLine("A的方法");
}
class B : A
{
public B() => Console.WriteLine("B的構(gòu)造函數(shù)");
public void Fun() => Console.WriteLine("B的方法");
}
class Program
{
static void Main(string[] args)
{
A a = new A();
a.Fun();
Console.ReadLine();
}
}
上述Main方法中在new A對(duì)象時(shí),程序首先進(jìn)入class A中,執(zhí)行class A的構(gòu)造函數(shù)A(),然后執(zhí)行class A中的Fun()方法,故運(yùn)行結(jié)果為:

場(chǎng)景二
實(shí)例化子類,子類和父類的構(gòu)造函數(shù)的執(zhí)行順序:當(dāng)執(zhí)行B對(duì)象時(shí),因?yàn)槔^承A對(duì)象,所以首先執(zhí)行基類A的構(gòu)造函數(shù)
class A
{
public A() => Console.WriteLine("A的構(gòu)造函數(shù)");
public virtual void Fun()=> Console.WriteLine("A的方法");
}
class B : A
{
public B() => Console.WriteLine("B的構(gòu)造函數(shù)");
public void Fun() => Console.WriteLine("B的方法");
}
class Program
{
static void Main(string[] args)
{
B b = new B();
b.Fun();
Console.ReadKey();
}
}
上述Main方法中在new B對(duì)象時(shí),由于B繼承A,先執(zhí)行父類的構(gòu)造函數(shù),所以先執(zhí)行A中的構(gòu)造函數(shù)A(),然后在執(zhí)行B中的構(gòu)造函數(shù)B(),故運(yùn)行結(jié)果為:

場(chǎng)景三
父類有虛方法,子類沒有使用(override)關(guān)鍵字重寫父類方法的時(shí)候,使用的是new關(guān)鍵字時(shí):
class A
{
public A()=> Console.WriteLine("A的構(gòu)造函數(shù)");
public virtual void Fun() => Console.WriteLine("A的方法");
}
class B : A
{
public B() => Console.WriteLine("B的構(gòu)造函數(shù)");
//不寫new時(shí),該方法會(huì)拋出警告,但不是錯(cuò)誤
public new void Fun()=> Console.WriteLine("B的方法");
}
class Program
{
static void Main(string[] args)
{
A a = new B();
a.Fun();
Console.ReadKey();
}
}
上述Main方法中先new B對(duì)象,先執(zhí)行A中的構(gòu)造函數(shù)A(),然后在執(zhí)行B中的構(gòu)造函數(shù)B(),最后調(diào)用class A的Fun()方法(沒有重寫父類方法),故運(yùn)行結(jié)果為:

場(chǎng)景四
父類有虛方法, 當(dāng)子類重寫了(override)父類的方法時(shí):
class A
{
public A()=> Console.WriteLine("A的構(gòu)造函數(shù)");
public virtual void Fun() => Console.WriteLine("A的方法");
}
class B : A
{
public B()=> Console.WriteLine("B的構(gòu)造函數(shù)");
public override void Fun()=> Console.WriteLine("B的方法");
}
static void Main(string[] args)
{
A a = new B();
a.Fun();
Console.ReadKey();
}
上述Main方法同樣是先new B對(duì)象,先執(zhí)行A中的構(gòu)造函數(shù)A(),然后在執(zhí)行B中的構(gòu)造函數(shù)B(),但是子方法中使用了override關(guān)鍵字“覆蓋”,使得子類中方法覆蓋了父類中的方法,無法再訪問父類中原始方法。(要重寫方法,父類方法必須有virtual關(guān)鍵字),所以其運(yùn)行結(jié)果為:

場(chǎng)景五
基類是接口層,多重繼承時(shí):
interface I
{
void Fun();
}
class A : I
{
public A() => Console.WriteLine("A的構(gòu)造函數(shù)");
public virtual void Fun() => Console.WriteLine("A的方法");
}
class B : A
{
public B() => Console.WriteLine("B的構(gòu)造函數(shù)");
//不寫new時(shí),該方法會(huì)拋出警告
public new void Fun() =>Console.WriteLine("B的方法");
}
static void Main(string[] args)
{
B b = new B();
b.Fun();
((A)b).Fun();
((I)b).Fun();
Console.ReadKey();
}
打印結(jié)果:

場(chǎng)景六
當(dāng)多重繼承,子類重寫override父類方法時(shí):
interface I
{
void Fun();
}
class A : I
{
public A() => Console.WriteLine("A的構(gòu)造函數(shù)");
public virtual void Fun() => Console.WriteLine("A的方法");
}
class B : A
{
public B() => Console.WriteLine("B的構(gòu)造函數(shù)");
public override void Fun() =>Console.WriteLine("B的方法");
}
static void Main(string[] args)
{
B b = new B();
b.Fun();
((A)b).Fun();
((I)b).Fun();
Console.ReadKey();
}
打印結(jié)果:(對(duì)比場(chǎng)景5)

場(chǎng)景七
使用new重寫父類方法,同時(shí)讓每個(gè)子類都繼承接口:
interface I
{
void Fun();
}
class A : I
{
public A() => Console.WriteLine("A的構(gòu)造函數(shù)");
public virtual void Fun() => Console.WriteLine("A的方法");
}
class B : A, I
{
public B() => Console.WriteLine("B的構(gòu)造函數(shù)");
//不寫new時(shí),該方法會(huì)拋出警告
public new void Fun() => Console.WriteLine("B的方法");
}
static void Main(string[] args)
{
B b = new B();
b.Fun();
((A)b).Fun();
((I)b).Fun();
Console.ReadKey();
}
打印結(jié)果:

到此這篇關(guān)于C#類中方法的執(zhí)行順序是什么的文章就介紹到這了,更多相關(guān)C#類中方法的執(zhí)行順序 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#類繼承中構(gòu)造函數(shù)的執(zhí)行序列示例詳解
- C#類的多態(tài)性詳解
- C#類中static變量用法分析
- C#類的訪問修飾符用法分析
- c#類的使用示例
- C#類中的屬性使用總結(jié)(詳解類的屬性)
- C#類中屬性與成員變量的使用小結(jié)
- c#對(duì)象初始化順序?qū)嵗治?/a>
- C#對(duì)象為Null模式(Null Object Pattern)實(shí)例教程
- c#對(duì)象反序列化與對(duì)象序列化示例詳解
- C#對(duì)象與XMl文件之間的相互轉(zhuǎn)換
- 自定義實(shí)現(xiàn)Json字符串向C#對(duì)象轉(zhuǎn)變的方法
- 關(guān)于C# 類和對(duì)象詳情
相關(guān)文章
基于Unity實(shí)現(xiàn)2D邊緣檢測(cè)
這篇文章主要介紹了如何利用Unity實(shí)現(xiàn)2D邊緣檢測(cè),從而達(dá)到人物描邊效果。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04
C#中結(jié)構(gòu)體定義并轉(zhuǎn)換字節(jié)數(shù)組詳解
在寫C#TCP通信程序時(shí),發(fā)送數(shù)據(jù)時(shí),只能發(fā)送byte數(shù)組,處理起來比較麻煩不說,如果是和VC6.0等寫的程序通信的話,很多的都是傳送結(jié)構(gòu)體,在VC6.0中可以很方便的把一個(gè)char[]數(shù)組轉(zhuǎn)換為一個(gè)結(jié)構(gòu)體,而在C#卻不能直接把byte數(shù)組轉(zhuǎn)換為結(jié)構(gòu)體,要在C#中發(fā)送結(jié)構(gòu)體,應(yīng)該怎么做呢?2017-11-11
C#中的Image控件用法詳解與實(shí)際應(yīng)用示例
在C#應(yīng)用程序開發(fā)中,圖像顯示是一個(gè)常見的需求,無論是創(chuàng)建圖形界面還是處理圖像數(shù)據(jù),System.Windows.Controls.Image控件都是實(shí)現(xiàn)這一目標(biāo)的重要工具,本文將詳細(xì)介紹Image控件的功能、用法、優(yōu)化技巧以及一些實(shí)際應(yīng)用示例,需要的朋友可以參考下2024-06-06
Unity C#打包AssetBundle與場(chǎng)景詳解
這篇文章主要給大家介紹了關(guān)于Unity C#打包AssetBundle與場(chǎng)景的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02

