110,535
社区成员
发帖
与我相关
我的任务
分享
public Employee(string name): this("aaa", 5000)//调用两个参数的构造函数,真不知道this中改怎么写,索性就赋值了
{
this.name = name;
}
public Employee(string name, int salary)
{
this.name = name;
this.salary = salary;
}
//ok,开始调用构造函数实例化对象
Employee w2= new Employee("bbb");//很显然的被调用的构造函数中的属性(aaa)会被覆盖掉
//说明了什么?构造函数调用构造函数目的根本不在此
public Employee(string name, int salary)
{
this.educational = "Undergraduate ";//假如类中有一个学历信息,这里且认为都是本科教育程度
this.name = name;
this.salary = salary;
}
//那么在构造调用构造的时候,
public Employee(string name): this("aaa", 5000)
{
this.name = name;
}
//用这个构造实例化的对象“学历”这一属性都将是本科
//这仅仅是我的理解,可能不对,但是我认为构造函数的相互调用绝不是赋值那么简单
//OK,现在问题又出来了,既然需要所有对象都是“本科”这一学历,那么为什么不把“学历”这一属性
//直接定义为静态的且初始化的状态(“本科”)?似乎我自己又推翻了自己的结论
//算了,公司的代码不贴了
public Employee(string name)
{
this.name = name;
}
public Employee(string name, int salary): this(name)
{
this.salary = salary;
}
public Employee(string name): this("aaa", 5000){}
public Employee(string name, int salary)
{
this.name = name;
this.salary = salary;
}