如代码,未避免写三行重复代码,使用this关键字调用无参构造方法
public class Person
{
public string name;
public int age;
public char gender;
public double height;
public double weight;
public float score;
//无参构造方法
public Person() { }
//有参构造方法之给名字,年龄,性别赋值
public Person(string name,int age, char gender)
{
this.name = name;
this.age = age;
this.gender = gender;
}
//有参构造方法值给名字,年龄,性别,身高,体重,分数赋值。
public Person(string name,int age,char gender,double height,double weight,float score):this(name,age,gender)
{
//以下三行代码与之前的代码重复,如何解决代码冗余呢?则使用this关键字先调用无参构造方法
//this.name = name;
//this.age = age;
//this.gender = gender;
this.height = height;
this.weight = weight;
this.score = score;
}
}
public class Program
{
public static void Main(string[] args)
{
// 实例化一个对象小明
Person xiaoming = new Person("xiaoming",19,'G');
}
}