c# base關(guān)鍵字的具體使用
1. 調(diào)用父類的構(gòu)造函數(shù)
class Father { public int Age { get; set; } public string Name { get; set; } public Father(int age,string name) { this.Age = age; this.Name = name; } } class Son:Father { public int Height { get; set; } public Son(int age,string name,int height):base (age,name) { this.Height = height; } } class grandson : Son { public grandson(int age, string name, int height) : base(age, name ) { this.Height = height; } }
會(huì)發(fā)現(xiàn)上面的代碼報(bào)錯(cuò),因?yàn)間randson 這個(gè)類的構(gòu)造函數(shù)使用base調(diào)用父類構(gòu)造函數(shù)時(shí),提示缺少了height這個(gè)參數(shù),這是因?yàn)閎ase調(diào)用的只是它的父類也就是Son這個(gè)類的構(gòu)造函數(shù),而不是最頂層的Father這個(gè)類的構(gòu)造函數(shù),所以這里報(bào)錯(cuò)改成如下代碼即可:
class Father { public int Age { get; set; } public string Name { get; set; } public Father(int age,string name) { this.Age = age; this.Name = name; } } class Son:Father { public int Height { get; set; } public Son(int age,string name,int height):base (age,name) { this.Height = height; } } class grandson : Son { public grandson(int age, string name, int height) : base(age, name,height ) { this.Height = height; } }
2. 調(diào)用父類的方法或者屬性
class Father { public int Age { get; set; } public string Name { get; set; } public Father(int age, string name) { this.Age = age; this.Name = name; } public void Test() { Console.WriteLine("我是Father"); } } class Son : Father { public int Height { get; set; } public Son(int age, string name, int height) : base(age, name) { this.Height = height; } public void Test() { Console.WriteLine("我是Son"); } } class grandson : Son { public grandson(int age, string name, int height) : base(age, name, height) { this.Height = height; } public void Show() { int age = base.Age; base.Test(); } }
調(diào)用:
grandson father = new grandson(100, "小明", 180); father.Show();
輸出:
我是Son
可以看出調(diào)用的方法不是father類中的test方法,而是Son類,也就是grandson的父類的方法。
到此這篇關(guān)于c# base關(guān)鍵字的具體使用的文章就介紹到這了,更多相關(guān)c# base關(guān)鍵字內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中調(diào)用Windows API的技術(shù)要點(diǎn)說明
本篇文章主要是對(duì)C#中調(diào)用Windows API的技術(shù)要點(diǎn)進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01探討:如何使用委托,匿名方法對(duì)集合進(jìn)行萬能排序
本篇文章是對(duì)使用委托,匿名方法對(duì)集合進(jìn)行萬能排序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06C#使用Winform連接SQL Server數(shù)據(jù)庫的詳細(xì)步驟
Windows Forms是一個(gè)用于構(gòu)建Windows桌面應(yīng)用程序的圖形用戶界面(GUI)庫,它是.NET Framework的一部分,允許開發(fā)者快速創(chuàng)建豐富的交互式界面,本文給大家介紹了C#使用Winforms連接SQL Server數(shù)據(jù)庫的詳細(xì)步驟,需要的朋友可以參考下2024-09-09C# 運(yùn)算符 ?、??、?: 各種問號(hào)的用法和說明
本文介紹C#中三種常見的問號(hào)運(yùn)算符的使用方法,簡單講解給大家,希望對(duì)大家有所幫助。2016-04-04C#實(shí)現(xiàn)JSON和對(duì)象之間互相轉(zhuǎn)換功能示例
這篇文章主要介紹了C#實(shí)現(xiàn)JSON和對(duì)象之間互相轉(zhuǎn)換功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了C#實(shí)現(xiàn)對(duì)象與json之間相互轉(zhuǎn)換的操作技巧,需要的朋友可以參考下2017-09-09C# OpenCvSharp實(shí)現(xiàn)去除字母后面的雜線
這篇文章主要為大家詳細(xì)介紹了C#如何使用OpenCvSharp實(shí)現(xiàn)去除字母后面的雜線效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11C#獲取客戶端相關(guān)信息實(shí)例總結(jié)
這篇文章主要介紹了C#獲取客戶端相關(guān)信息的方法,以實(shí)例形式總結(jié)了C#獲取客戶端IP地址、網(wǎng)絡(luò)連接、硬件信息等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09c# 數(shù)據(jù)標(biāo)注與數(shù)據(jù)校驗(yàn)
這篇文章主要介紹了c# 數(shù)據(jù)標(biāo)注與數(shù)據(jù)校驗(yàn)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2020-10-10C# NetRemoting實(shí)現(xiàn)雙向通信
本篇文章主要介紹了C# NetRemoting實(shí)現(xiàn)雙向通信,.Net Remoting 是由客戶端通過Remoting,訪問通道以獲得服務(wù)端對(duì)象,再通過代理解析為客戶端對(duì)象來實(shí)現(xiàn)通信的2017-03-03