this与base
当前类的对象,可以通过this关键字调用相关属性或者方法。
class Employee
{
public string Name;
public stirng Sex;
public Person(string Name, string Sex)
{
this.Name = Name; //this.Name是前面定义的,并不是参数Name
this.Sex = Sex;
}
}
base 关键字用于从派生类中访问基类的成员。
using System;
public class Person
{
protected string ssn = "444-55-6666";
protected string name = "John L. Malgraine";
public virtual void GetInfo()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("SSN: {0}", ssn);
}
}
class Employee : Person
{
public string id = "ABC567EFG";
public override void GetInfo()
{
base.GetInfo(); //调用基类的方法GetInfo()
Console.WriteLine("Employee ID: {0}", id);
}
}
class TestClass
{
static void Main()
{
Employee E = new Employee();
E.GetInfo();
}
}