构造函数中base与this的区别

时间:2021-12-06 19:30:08

base是对父类的引用,而this是对类本身的引用。

namespace ConsoleApplication1
{
public class BaseClass
{
private string name;
private int age;
private string addr;
public BaseClass()
{
Console.WriteLine("No Info");
}
public BaseClass(string name)
{
this.name = name;
Console.WriteLine("name=" + this.name);
}
public BaseClass(string name, int age)
{
this.name = name;
this.age = age;
Console.WriteLine(string.Format("name={0},age={1}" ,this.name,this.age));
}

public BaseClass(string name, int age,string addr)
{
this.name = name;
this.age = age;
this.addr = addr;
Console.WriteLine(string.Format("name={0},age={1},addr={2}", this.name, this.age, this.addr));
}
}

public class ItemClass : BaseClass
{
private string name;
private int age;

public ItemClass()
: this("demo")
{
//Console.WriteLine("No Info");
}
public ItemClass(string name)
: this("jimmy", 20)
{
this.name = name;
Console.WriteLine("name=" + this.name);
}
public ItemClass(string name, int age)
{
this.name = name;
this.age = age;
Console.WriteLine(string.Format("name={0},age={1}", this.name, this.age));
}
public ItemClass(string name,int age,string addr)
: base("jeason", 20,"shanghai")
{
Console.WriteLine("name=" + name);
}

public static void Main()
{
ItemClass ic1 = new ItemClass("mike");
ItemClass ic2 = new ItemClass();
ItemClass ic3 = new ItemClass("mike",21,"chengdu");
}
}
} 

子类与基类的构造函数执行顺序:

先调用父类的构造函数进行初始化,再调用继承类的构造函数,如果没有在继承类中指明父类的构造函数,则默认调用父类中无参构造函数,然后调用继承类的构造函数。