C#類中方法的執(zhí)行順序是什么
有些中級(jí)開發(fā)小伙伴還是搞不太明白在繼承父類以及不同場(chǎng)景實(shí)例化的情況下,父類和子類的各種方法的執(zhí)行順序到底是什么,下面通過(guò)場(chǎng)景的舉例來(lái)重新認(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)景三
父類有虛方法,子類沒(méi)有使用(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()方法(沒(méi)有重寫父類方法),故運(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)鍵字“覆蓋”,使得子類中方法覆蓋了父類中的方法,無(wú)法再訪問(wè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#類的訪問(wèn)修飾符用法分析
- 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)文章
c# 動(dòng)態(tài)加載dll文件,并實(shí)現(xiàn)調(diào)用其中的方法(推薦)
下面小編就為大家?guī)?lái)一篇c# 動(dòng)態(tài)加載dll文件,并實(shí)現(xiàn)調(diào)用其中的方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02WinForm實(shí)現(xiàn)跨進(jìn)程通信的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)跨進(jìn)程通信的方法,通過(guò)一個(gè)WinMessageHelper類實(shí)現(xiàn)這一功能,需要的朋友可以參考下2014-08-08WinForm實(shí)現(xiàn)頁(yè)面按鈕定時(shí)隱藏功能
這篇文章主要介紹了WinForm實(shí)現(xiàn)頁(yè)面按鈕定時(shí)隱藏功能,結(jié)合實(shí)例形式分析了WinForm基于定時(shí)器的頁(yè)面控件屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05