c#派生类继承构造函数
来源:互联网 发布:hadoop数据挖掘实例 编辑:程序博客网 时间:2024/05/28 11:50
大家都知道C#构造函数主要用来设置类中属性的初始值,但经常会忽视类的构造方法也可以象方法一样引用调用父类中的构造方法或本身的其他构造方法。往往因此写了很多重复代码。下面的代码介绍了类的构造方法的几种用法。
知识点:调用父类base构造函数想必大家都知道,但是调用自身 :this(param…)构造函数不一定都了解,这里主要突出这个知识点。
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>
/// 一个参数构造方法
/// </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>
/// 重新ToString方法
/// </summary>
/// <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
参考文献:http://www.cnblogs.com/panyantao/archive/2010/12/30/1921454.html
- c#派生类继承构造函数
- c#基础-继承,派生类,派生类的构造 方法
- 多重继承派生类的构造函数
- 多重继承派生类的构造函数
- 多重继承派生类的构造函数
- 派生类构造函数-多继承
- 继承与派生:派生类的构造函数
- 继承与派生:虚基类及其派生类的构造函数
- 三十九、继承与派生:派生类的构造函数****
- C++构造函数 & 拷贝构造函数 & 派生类的构造函数 & 虚继承的构造函数
- C++构造函数 & 拷贝构造函数 & 派生类的构造函数 & 虚继承的构造函数
- C++构造函数 & 拷贝构造函数 & 派生类的构造函数 & 虚继承的构造函数
- c++ 单继承派生类的构造函数
- 7.27多重继承派生类的构造函数
- 多重继承中派生类的构造函数
- 派生类构造函数和多重继承的二义性问题
- 类的继承与派生中关于构造函数
- C++ 虚继承派生类构造函数的写法
- 制作一体化的安装包
- GIS制作地理底图/变化监测
- hive理解streamtable使用
- 云计算阅读笔记
- IOS判断版本之编译期和运行时
- c#派生类继承构造函数
- FLEX 与 .net wcf /web services通信
- Visual Studio源码管理器Team Foundation Service
- Visual Studio源码管理器Team Foundation Service
- 源代码管理:TFS
- 源代码管理:TFS
- Silverlight+WCF双工通信开发聊天室精简版服务器端代码
- Silverlight+WCF双工通信开发聊天室精简版服务器端代码
- silverlight中Wcf双工通信