public class Test
{
private string scope = "类的字段";
public string getResult()
{
string scope = "局部变量";
// this.scope = "类的字段" scope = "局部变量"
return this.scope + "-" + scope;
}
}
2. 串联构造函数。(一个构造函数调用其他构造函数)
using System;
public class Person
{
public Person()
{
Console.WriteLine("无参构造函数");
}
// this()对应无参构造方法Person()
// 先执行Person(),后执行Person(string name)
public Person(string name) : this()
{
Console.WriteLine("create person, name:{0}", name);
Console.WriteLine("有参构造函数");
}
}
class Program
{
static void Main(string[] args)
{
Person zhangsan = new Person("张三");
}
}