新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#繼承構(gòu)造函數(shù)實現(xiàn)淺析
C#繼承構(gòu)造函數(shù)存在的意義:大家都知道C#構(gòu)造函數(shù)主要用來設置類中屬性的初始值,但經(jīng)常會忽視類的構(gòu)造方法也可以象方法一樣引用調(diào)用父類中的構(gòu)造方法或本身的其他構(gòu)造方法。往往因此寫了很多重復代碼。下面的代碼介紹了類的構(gòu)造方法的幾種用法。

成都創(chuàng)新互聯(lián)公司秉承實現(xiàn)全網(wǎng)價值營銷的理念,以專業(yè)定制企業(yè)官網(wǎng),成都網(wǎng)站建設、成都網(wǎng)站制作,成都小程序開發(fā),網(wǎng)頁設計制作,手機網(wǎng)站制作設計,全網(wǎng)營銷推廣幫助傳統(tǒng)企業(yè)實現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級專業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對客戶都以感恩的心態(tài)奉獻自己的專業(yè)和所長。
C#繼承構(gòu)造函數(shù)的示例:
- using System;
- namespace TestApplication
- {
- class Test
- {
- static void Main(string[] args)
- {
- TestA testA1 = new TestA();
- Console.WriteLine("測試類A無參數(shù)構(gòu)造方法");
- Console.WriteLine(testA1.ToString());
- Console.WriteLine();
- TestA testA2 = new TestA("Set First Param");
- Console.WriteLine("測試類A一個參數(shù)構(gòu)造方法");
- Console.WriteLine(testA2.ToString());
- Console.WriteLine();
- TestB testB1= new TestB();
- Console.WriteLine("測試類B無參數(shù)構(gòu)造方法");
- Console.WriteLine(testB1.ToString());
- Console.WriteLine();
- TestB testB2 = new TestB("Set First Param");
- Console.WriteLine("測試類B一個參數(shù)構(gòu)造方法");
- Console.WriteLine(testB2.ToString());
- Console.WriteLine();
- TestB testB3 = new TestB("Set First Param", "Set Second Param");
- Console.WriteLine("測試類B兩個參數(shù)構(gòu)造方法");
- Console.WriteLine(testB3.ToString());
- Console.WriteLine();
- TestB testB4 = new TestB("Set First Param",
- "Set Second Param", "Set Third Param");
- Console.WriteLine("測試類B三個參數(shù)構(gòu)造方法");
- Console.WriteLine(testB4.ToString());
- Console.WriteLine();
- Console.ReadLine();
- }
- }
- ///
- /// 測試類A---C#繼承構(gòu)造函數(shù)
- ///
- class TestA
- {
- protected string _testValueA;
- ///
- /// 無參數(shù)構(gòu)造方法 --C#繼承構(gòu)造函數(shù)
- ///
- public TestA():this("Set First Param")
- {
- }
- ///
- /// 一個參數(shù)構(gòu)造方法 --C#繼承構(gòu)造函數(shù)
- ///
- ///
- public TestA(string value)
- {
- _testValueA = value;
- }
- ///
- /// 重新ToString方法
- ///
- ///
- public override string ToString()
- {
- return this._testValueA;
- }
- }
- ///
- /// 測試類TestB,從TestA類中繼承---C#繼承構(gòu)造函數(shù)
- ///
- class TestB : TestA
- {
- protected string _testValueB;
- protected string _testValueC;
- ///
- /// 調(diào)用父類中的構(gòu)造方法
- ///
- public TestB():base()
- {
- this._testValueB = "Set Second Param";
- this._testValueC = "Set Third Param";
- }
- ///
- /// 調(diào)用父類中的構(gòu)造方法--C#繼承構(gòu)造函數(shù)
- ///
- ///
- public TestB(string valueA)
- : base(valueA)
- {
- this._testValueB = "Set Second Param";
- this._testValueC = "Set Third Param";
- }
- ///
- /// 調(diào)用其他構(gòu)造方法---C#繼承構(gòu)造函數(shù)
- ///
- ///
- ///
- public TestB(string valueA, string valueB)
- : this(valueA, valueB, "Set Third Param")
- {
- }
- ///
- /// 三個參數(shù)的構(gòu)造方法
- ///
- ///
- ///
- ///
- public TestB(string valueA, string valueB, string valueC)
- {
- this._testValueA = valueA;
- this._testValueB = valueB;
- this._testValueC = valueC;
- }
- ///
- /// 重新ToString方法 --C#繼承構(gòu)造函數(shù)
- ///
- ///
- public override string ToString()
- {
- return this._testValueA + "\n" + this._testValueB + "\n" + this._testValueC ;
- }
- }
- }
C#繼承構(gòu)造函數(shù)示例輸出結(jié)果:
- 測試類A無參數(shù)構(gòu)造方法
- Set First Param
- 測試類A一個參數(shù)構(gòu)造方法
- Set First Param
- 測試類B無參數(shù)構(gòu)造方法
- Set First Param
- Set Second Param
- Set Third Param
- 測試類B一個參數(shù)構(gòu)造方法
- Set First Param
- Set Second Param
- Set Third Param
- 測試類B兩個參數(shù)構(gòu)造方法
- Set First Param
- Set Second Param
- Set Third Param
- 測試類B三個參數(shù)構(gòu)造方法
- Set First Param
- Set Second Param
- Set Third Param
C#繼承構(gòu)造函數(shù)的基本情況就向你介紹到這里,希望對你學習和了解C#繼承構(gòu)造函數(shù)有所幫助。
【編輯推薦】
- C#構(gòu)造函數(shù)與C++的區(qū)別淺析
- C#構(gòu)造函數(shù)的運用淺析
- 學習C#構(gòu)造函數(shù)的一點體會
- C#靜態(tài)構(gòu)造函數(shù)特點淺析
- C#靜態(tài)構(gòu)造函數(shù)學習心得淺析
分享名稱:C#繼承構(gòu)造函數(shù)實現(xiàn)淺析
網(wǎng)站URL:http://m.5511xx.com/article/dhpcdcg.html


咨詢
建站咨詢
