不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
C#重载构造函数的调用
编辑:dnawo 日期:2009-01-20
复制内容到剪贴板
程序代码

public class Student
{
private string _Name;
private int _Age;
private int _Sex;
/// <summary>
/// 构造函数
/// </summary>
public Student():this("",0,0)
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name"></param>
public Student(string name):this(name,10,1)
{}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name"></param>
/// <param name="age"></param>
public Student(string name, int age):this(name, age, 2)
{ }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name"></param>
/// <param name="age"></param>
/// <param name="sex"></param>
public Student(string name, int age, int sex)
{
_Name = name;
_Age = age;
_Sex = sex;
}
/// <summary>
/// 姓名
/// </summary>
public string Name
{
get { return _Name; }
set { _Name = value; }
}
/// <summary>
/// 年龄
/// </summary>
public int Age
{
get { return _Age; }
set { _Age = value; }
}
/// <summary>
/// 性别
/// </summary>
public int Sex
{
get { return _Sex; }
set { _Sex = value; }
}
}
{
private string _Name;
private int _Age;
private int _Sex;
/// <summary>
/// 构造函数
/// </summary>
public Student():this("",0,0)
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name"></param>
public Student(string name):this(name,10,1)
{}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name"></param>
/// <param name="age"></param>
public Student(string name, int age):this(name, age, 2)
{ }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name"></param>
/// <param name="age"></param>
/// <param name="sex"></param>
public Student(string name, int age, int sex)
{
_Name = name;
_Age = age;
_Sex = sex;
}
/// <summary>
/// 姓名
/// </summary>
public string Name
{
get { return _Name; }
set { _Name = value; }
}
/// <summary>
/// 年龄
/// </summary>
public int Age
{
get { return _Age; }
set { _Age = value; }
}
/// <summary>
/// 性别
/// </summary>
public int Sex
{
get { return _Sex; }
set { _Sex = value; }
}
}
调用示例:
复制内容到剪贴板
程序代码

Student student1 = new Student();
Response.Write(string.Format("Name:{0},Age:{1},Sex:{2}<br/>",student1.Name,student1.Age,student1.Sex));
Student student2 = new Student("dnawo");
Response.Write(string.Format("Name:{0},Age:{1},Sex:{2}<br/>", student2.Name, student2.Age, student2.Sex));
Student student3 = new Student("dnawo", 20);
Response.Write(string.Format("Name:{0},Age:{1},Sex:{2}<br/>", student3.Name, student3.Age, student3.Sex));
Response.Write(string.Format("Name:{0},Age:{1},Sex:{2}<br/>",student1.Name,student1.Age,student1.Sex));
Student student2 = new Student("dnawo");
Response.Write(string.Format("Name:{0},Age:{1},Sex:{2}<br/>", student2.Name, student2.Age, student2.Sex));
Student student3 = new Student("dnawo", 20);
Response.Write(string.Format("Name:{0},Age:{1},Sex:{2}<br/>", student3.Name, student3.Age, student3.Sex));
结果:

Name:,Age:0,Sex:0
Name:dnawo,Age:10,Sex:1
Name:dnawo,Age:20,Sex:2
Name:dnawo,Age:10,Sex:1
Name:dnawo,Age:20,Sex:2
如上边例子所示,重载构造函数的调用必须借助this关键字来进行,而不能像普通函数那样调用,否则将出错。
评论: 0 | 引用: 0 | 查看次数: 3581
发表评论
请登录后再发表评论!