1.base關鍵字用於從派生類中訪問基類的成員。
2.調用基類上已被其他方法重寫的方法。
3.指定創建派生類的實例是應調用基類的構造函數。
4.訪問基類的公有成員和受保護成員,不能訪問私有成員
5在靜態方法中用base關鍵字是錯誤的.。
下面的實例中 基類Person和派生類Employee都有一個GetInfo()方法,通過base關鍵字可以從派生類中調用基類的GetInfo()方法。
class TestBase
{
static void Main()
{
Employye employee = new Employye();
employee.GetInfo();
Console.Read();
}
}
public class Person
{
public string Name = "jing";
public int Age = 20;
public virtual void GetInfo()
{
Console.WriteLine("名字:{0},年齡:{1}",Name,Age);
}
}
public class Employye : Person
{
public string Id = "123456";
public override void GetInfo()
{
base.GetInfo();
Console.WriteLine("工號:{0}", Id);
}
}