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
/// </summary>
class TestA
{
protected string _testValueA;
/// <summary>
/
// ⽆参数构造⽅法
/// </summary>
public TestA():this("Set First Param")
{
}
/// <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类中继承
/// </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>
/// 调⽤⽗类中的构造⽅法
/// </summary>
/// <param name="valueA"></param>
public TestB(string valueA)
: base(valueA)
{
this._testValueB = "Set Second Param";
this._testValueC = "Set Third Param";
}
/// <summary>
/// 调⽤其他构造⽅法
/// </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>
writeline函数
/// 重新ToString⽅法
/// <returns></returns>
public override string ToString()
{
return this._testValueA + "\n" + this._testValueB + "\n" + this._testValueC ; }
}
}
输出结果:
测试类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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。