C#繼承構(gòu)造函數(shù)實(shí)現(xiàn)淺析
C#繼承構(gòu)造函數(shù)存在的意義:大家都知道C#構(gòu)造函數(shù)主要用來設(shè)置類中屬性的初始值,但經(jīng)常會(huì)忽視類的構(gòu)造方法也可以象方法一樣引用調(diào)用父類中的構(gòu)造方法或本身的其他構(gòu)造方法。往往因此寫了很多重復(fù)代碼。下面的代碼介紹了類的構(gòu)造方法的幾種用法。
C#繼承構(gòu)造函數(shù)的示例:
- using System;
- namespace TestApplication
- {
- class Test
- {
- static void Main(string[] args)
- {
- TestA testA1 = new TestA();
- Console.WriteLine("測(cè)試類A無參數(shù)構(gòu)造方法");
- Console.WriteLine(testA1.ToString());
- Console.WriteLine();
- TestA testA2 = new TestA("Set First Param");
- Console.WriteLine("測(cè)試類A一個(gè)參數(shù)構(gòu)造方法");
- Console.WriteLine(testA2.ToString());
- Console.WriteLine();
- TestB testB1= new TestB();
- Console.WriteLine("測(cè)試類B無參數(shù)構(gòu)造方法");
- Console.WriteLine(testB1.ToString());
- Console.WriteLine();
- TestB testB2 = new TestB("Set First Param");
- Console.WriteLine("測(cè)試類B一個(gè)參數(shù)構(gòu)造方法");
- Console.WriteLine(testB2.ToString());
- Console.WriteLine();
- TestB testB3 = new TestB("Set First Param", "Set Second Param");
- Console.WriteLine("測(cè)試類B兩個(gè)參數(shù)構(gòu)造方法");
- Console.WriteLine(testB3.ToString());
- Console.WriteLine();
- TestB testB4 = new TestB("Set First Param",
- "Set Second Param", "Set Third Param");
- Console.WriteLine("測(cè)試類B三個(gè)參數(shù)構(gòu)造方法");
- Console.WriteLine(testB4.ToString());
- Console.WriteLine();
- Console.ReadLine();
- }
- }
- /// <summary>
- /// 測(cè)試類A---C#繼承構(gòu)造函數(shù)
- /// </summary>
- class TestA
- {
- protected string _testValueA;
- /// <summary>
- /// 無參數(shù)構(gòu)造方法 --C#繼承構(gòu)造函數(shù)
- /// </summary>
- public TestA():this("Set First Param")
- {
- }
- /// <summary>
- /// 一個(gè)參數(shù)構(gòu)造方法 --C#繼承構(gòu)造函數(shù)
- /// </summary>
- /// <param name="value"></param>
- public TestA(string value)
- {
- _testValueA = value;
- }
- /// <summary>
- /// 重新ToString方法
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- return this._testValueA;
- }
- }
- /// <summary>
- /// 測(cè)試類TestB,從TestA類中繼承---C#繼承構(gòu)造函數(shù)
- /// </summary>
- class TestB : TestA
- {
- protected string _testValueB;
- protected string _testValueC;
- /// <summary>
- /// 調(diào)用父類中的構(gòu)造方法
- /// </summary>
- public TestB():base()
- {
- this._testValueB = "Set Second Param";
- this._testValueC = "Set Third Param";
- }
- /// <summary>
- /// 調(diào)用父類中的構(gòu)造方法--C#繼承構(gòu)造函數(shù)
- /// </summary>
- /// <param name="valueA"></param>
- public TestB(string valueA)
- : base(valueA)
- {
- this._testValueB = "Set Second Param";
- this._testValueC = "Set Third Param";
- }
- /// <summary>
- /// 調(diào)用其他構(gòu)造方法---C#繼承構(gòu)造函數(shù)
- /// </summary>
- /// <param name="valueA"></param>
- /// <param name="valueB"></param>
- public TestB(string valueA, string valueB)
- : this(valueA, valueB, "Set Third Param")
- {
- }
- /// <summary>
- /// 三個(gè)參數(shù)的構(gòu)造方法
- /// </summary>
- /// <param name="valueA"></param>
- /// <param name="valueB"></param>
- /// <param name="valueC"></param>
- public TestB(string valueA, string valueB, string valueC)
- {
- this._testValueA = valueA;
- this._testValueB = valueB;
- this._testValueC = valueC;
- }
- /// <summary>
- /// 重新ToString方法 --C#繼承構(gòu)造函數(shù)
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- return this._testValueA + "\n" + this._testValueB + "\n" + this._testValueC ;
- }
- }
- }
C#繼承構(gòu)造函數(shù)示例輸出結(jié)果:
- 測(cè)試類A無參數(shù)構(gòu)造方法
- Set First Param
- 測(cè)試類A一個(gè)參數(shù)構(gòu)造方法
- Set First Param
- 測(cè)試類B無參數(shù)構(gòu)造方法
- Set First Param
- Set Second Param
- Set Third Param
- 測(cè)試類B一個(gè)參數(shù)構(gòu)造方法
- Set First Param
- Set Second Param
- Set Third Param
- 測(cè)試類B兩個(gè)參數(shù)構(gòu)造方法
- Set First Param
- Set Second Param
- Set Third Param
- 測(cè)試類B三個(gè)參數(shù)構(gòu)造方法
- Set First Param
- Set Second Param
- Set Third Param
C#繼承構(gòu)造函數(shù)的基本情況就向你介紹到這里,希望對(duì)你學(xué)習(xí)和了解C#繼承構(gòu)造函數(shù)有所幫助。
【編輯推薦】