http://www.programfan.com/blog/article.asp?id=30049
看C#高级编程中关于类的构造函数时候碰到BASE和THIS不懂 看了下这篇文章,不错!
代码最具有说服性了,看下面代码吧:
//代码段一
public class ConstructorProgram1
{
private string name;
public ConstructorProgram1()
{
Console.WriteLine("No Info Left");
}
public ConstructorProgram1(string name)
{
this.name = name;
Console.WriteLine("name=" + this.name);
}
}
这里我重载了另外一个构造函数,里面的this作为一个指针指示了一个类的引用。即是ConstructorProgram1类的引用。这个很简单,但不是重点。
这篇文章最重点要说一下:this和base在构造函数中扮演的角色。
看下面的代码:
public class ConstructorProgram
{
private string name;
private int age;
public ConstructorProgram():this("bell")
{
//Console.WriteLine("No Info Left.");
}
public ConstructorProgram(string name)
:this("Simple Programmer",20)
{
this.name = name;
Console.WriteLine("name=" + this.name);
}
public ConstructorProgram(string name, int age)
{
this.name = name;
this.age = age;
Console.WriteLine("name=" + this.name);
Console.WriteLine("age=" + this.age);
}
public static void Main()
{
ConstructorProgram cp1= new ConstructorProgram("goal");
ConstructorProgram cp2 = new ConstructorProgram();
}
}
运行结果:
name=Simple Programmer
age=20
name=goal
name=Simple Programmer
age=20
name=bell
在上面的代码当中,可以看出来这里红色字体的this用来调用ConstructorProgram类的自身的构造函数。明白这句话的涵义,应该会明白执行结果了。
接着下面的代码如下:
public class ConstructorProgram1
{
private string name;
private int age;
public ConstructorProgram1()
{
Console.WriteLine("No Info Left");
}
public ConstructorProgram1(string name)
{
this.name = name;
Console.WriteLine("name=" + this.name);
}
public ConstructorProgram1(string name, int age)
{
this.name = name;
this.age = age;
Console.WriteLine("name=" + this.name);
Console.WriteLine("age=" + this.age);
}
}
public class ConstructorProgram : ConstructorProgram1
{
public ConstructorProgram()
{
//Console.WriteLine("sdfsdf");
}
public ConstructorProgram(string name)
: base("goalbell",20)
{
Console.WriteLine("name=" + name);
}
public static void Main()
{
ConstructorProgram cp = new ConstructorProgram("Programmer");
}
}
运行结果如下:
name=goalbell
age=20
name=Programmer
可以看出上面的代码,派生类中的base调用了父类(基类)的构造函数了,但是如果不提供初始化(即把 : base("goalbell",20)
不要)指向基类的构造函数的话,它会执行基类中没有参数的构造函数。
会得到如下结果:
name=goalbell
age=20
name=Programmer
No Info Left
也就是说BASE是对父类的引用,而THIS是对类本身自己的引用。
在看下下面的代码
using System;
namespace Zjw.Csharp
{
public class ConstructorProgram1
{
private string name;
public ConstructorProgram1()
{
Console.WriteLine("No Info Left");
}
public ConstructorProgram1(string name)
{
this.name = name;
Console.WriteLine("name=" + this.name);
}
}
public class ConstructorProgram:ConstructorProgram1
{
private string name;
private int age;
public ConstructorProgram():this("bell")
{
//Console.WriteLine("No Info Left.");
}
public ConstructorProgram(string name)
:this("Simple Programmer",20)
{
this.name = name;
Console.WriteLine("name=" + this.name);
}
public ConstructorProgram(string name, int age)
{
this.name = name;
this.age = age;
Console.WriteLine("name=" + this.name);
Console.WriteLine("age=" + this.age);
}
public static void Main()
{
ConstructorProgram cp1= new ConstructorProgram("goal");
ConstructorProgram cp2 = new ConstructorProgram();
Console.ReadLine();
}
}
}
执行结果是
No Info Left
name=Simple Programmer
age=20
name=goal
No Info Left
name=Simple Programmer
age=20
name=bell
可以得到的结论是 先调用父类的构造函数进行初始化,在调用继承类的构造函数,如果没有在继承类中指明父类的构造函数,则默认调用父类中没有参数的构造函数,然后调用继承类的构造函数。