this的一种用法可以指代整类。直接贴代码
public class Person(){
//一个参数时
public Person(string name ){
this.Name = name;
}
//一个参数时
public Person( int age){
this.Age = age;
}
//两个参数时
public Person(string name int age){
this.Name = name;
this.Age = age;
}
}
以上的需求是,想构建不同的构造函数,使得传进来不论是传进来几个参数都可以被使用
利用this进行函数重构,代码如下
public Person(string name ):this(0,name){
}
//一个参数时
public Person( int age):this(string.Empty,age){
}
//两个参数时
public Person(string name int age){
this.Name = name;
this.Age = age;
}
}
——————————————————————————————————————————————————————————————————————
base用法
首先应该注意,如果一个子类是由父类派生出来时,首先子类的会率先去调用父类的构造函数,然后再去调用自己的构造函数。前提是继承过来父类的构造函数必须没有参数。如果一旦父类中没有不带参数的构造函数是,就回报错。所以会用到base(),上源码
子类