C#繼承構造函數實現淺析
作者:佚名
C#繼承構造函數是什么呢?C#繼承構造函數存在的意義是什么呀?那么本文就向你介紹這方面的內容。
C#繼承構造函數存在的意義:大家都知道C#構造函數主要用來設置類中屬性的初始值,但經常會忽視類的構造方法也可以象方法一樣引用調用父類中的構造方法或本身的其他構造方法。往往因此寫了很多重復代碼。下面的代碼介紹了類的構造方法的幾種用法。
C#繼承構造函數的示例:
- using System;
- namespace TestApplication
- {
- class Test
- {
- static void Main(string[] args)
- {
- TestA testA1 = new TestA();
- Console.WriteLine("測試類A無參數構造方法");
- Console.WriteLine(testA1.ToString());
- Console.WriteLine();
- TestA testA2 = new TestA("Set First Param");
- Console.WriteLine("測試類A一個參數構造方法");
- Console.WriteLine(testA2.ToString());
- Console.WriteLine();
- TestB testB1= new TestB();
- Console.WriteLine("測試類B無參數構造方法");
- Console.WriteLine(testB1.ToString());
- Console.WriteLine();
- TestB testB2 = new TestB("Set First Param");
- Console.WriteLine("測試類B一個參數構造方法");
- Console.WriteLine(testB2.ToString());
- Console.WriteLine();
- TestB testB3 = new TestB("Set First Param", "Set Second Param");
- Console.WriteLine("測試類B兩個參數構造方法");
- Console.WriteLine(testB3.ToString());
- Console.WriteLine();
- TestB testB4 = new TestB("Set First Param",
- "Set Second Param", "Set Third Param");
- Console.WriteLine("測試類B三個參數構造方法");
- Console.WriteLine(testB4.ToString());
- Console.WriteLine();
- Console.ReadLine();
- }
- }
- /// <summary>
- /// 測試類A---C#繼承構造函數
- /// </summary>
- class TestA
- {
- protected string _testValueA;
- /// <summary>
- /// 無參數構造方法 --C#繼承構造函數
- /// </summary>
- public TestA():this("Set First Param")
- {
- }
- /// <summary>
- /// 一個參數構造方法 --C#繼承構造函數
- /// </summary>
- /// <param name="value"></param>
- public TestA(string value)
- {
- _testValueA = value;
- }
- /// <summary>
- /// 重新ToString方法
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- return this._testValueA;
- }
- }
- /// <summary>
- /// 測試類TestB,從TestA類中繼承---C#繼承構造函數
- /// </summary>
- class TestB : TestA
- {
- protected string _testValueB;
- protected string _testValueC;
- /// <summary>
- /// 調用父類中的構造方法
- /// </summary>
- public TestB():base()
- {
- this._testValueB = "Set Second Param";
- this._testValueC = "Set Third Param";
- }
- /// <summary>
- /// 調用父類中的構造方法--C#繼承構造函數
- /// </summary>
- /// <param name="valueA"></param>
- public TestB(string valueA)
- : base(valueA)
- {
- this._testValueB = "Set Second Param";
- this._testValueC = "Set Third Param";
- }
- /// <summary>
- /// 調用其他構造方法---C#繼承構造函數
- /// </summary>
- /// <param name="valueA"></param>
- /// <param name="valueB"></param>
- public TestB(string valueA, string valueB)
- : this(valueA, valueB, "Set Third Param")
- {
- }
- /// <summary>
- /// 三個參數的構造方法
- /// </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#繼承構造函數
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- return this._testValueA + "\n" + this._testValueB + "\n" + this._testValueC ;
- }
- }
- }
C#繼承構造函數示例輸出結果:
- 測試類A無參數構造方法
- Set First Param
- 測試類A一個參數構造方法
- Set First Param
- 測試類B無參數構造方法
- Set First Param
- Set Second Param
- Set Third Param
- 測試類B一個參數構造方法
- Set First Param
- Set Second Param
- Set Third Param
- 測試類B兩個參數構造方法
- Set First Param
- Set Second Param
- Set Third Param
- 測試類B三個參數構造方法
- Set First Param
- Set Second Param
- Set Third Param
C#繼承構造函數的基本情況就向你介紹到這里,希望對你學習和了解C#繼承構造函數有所幫助。
【編輯推薦】
責任編輯:仲衡
來源:
中國自學編程網