构造函数是用来创建对象的特殊方法
方法名和类名一样,没有返回值不用void
构造函数可以重载 就是有多个参数不同的构造函数
这是视频上的一个例子
namespace 构造函数
{
class Program
{
static void Main(string[] args)
{
person p1=new person("zs",18,"nan",80);
p1.SayHello();
Console.ReadKey();
}
}
class person
{
public person(string name, int age, string sex)//重载
{
this.name = name;
this.age = age;
this.sex = sex;
}
public person(string name, int age, string sex, int en)
{
this.name = name;
this.age = age;
this.sex = sex;
this.en = en;
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
private int en;
public int En
{
get { return en; }
set { en = value; }
}
public void SayHello()
{
Console.WriteLine("我叫{0},今年{1},我是{2}的,我的英语成绩{3}", this.name, this.age, this.sex, this.en);
}
}
}
|