base (C# 參考)base (C# Reference)

base 關鍵字是用來存取衍生類別中基底類別的成員︰The base keyword is used to access members of the base class from within a derived class:

  • 對已由另一個方法覆寫的基底類別來呼叫方法。Call a method on the base class that has been overridden by another method.

  • 指定應該在建立衍生類別的執行個體時呼叫的基底類別建構函式。Specify which base-class constructor should be called when creating instances of the derived class.

僅允許在建構函式、執行個體方法或執行個體屬性存取子中進行基底類別存取。A base class access is permitted only in a constructor, an instance method, or an instance property accessor.

從靜態方法使用 base 關鍵字是錯誤的。It is an error to use the base keyword from within a static method.

所存取的基底類別是類別宣告中所指定的基底類別。The base class that is accessed is the base class specified in the class declaration. 例如,如果您指定 class ClassB : ClassA,則不論 ClassA 的基底類別為何,都會從 ClassB 存取 ClassA 成員。For example, if you specify class ClassB : ClassA, the members of ClassA are accessed from ClassB, regardless of the base class of ClassA.

範例Example

在此範例中,基底類別 Person 和衍生類別 Employee 都會有名為 Getinfo 的方法。In this example, both the base class, Person, and the derived class, Employee, have a method named Getinfo. 使用 base 關鍵字,即可從衍生類別對基底類別呼叫 Getinfo 方法。By using the base keyword, it is possible to call the Getinfo method on the base class, from within the derived class.

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()
    {
        // Calling the base class GetInfo method:
        base.GetInfo();
        Console.WriteLine("Employee ID: {0}", id);
    }
}

class TestClass
{
    static void Main()
    {
        Employee E = new Employee();
        E.GetInfo();
    }
}
/*
Output
Name: John L. Malgraine
SSN: 444-55-6666
Employee ID: ABC567EFG
*/

如需其他範例,請參閱 newvirtualoverrideFor additional examples, see new, virtual, and override.

範例Example

這個範例示範如何指定在建立衍生類別的執行個體時呼叫的基底類別建構函式。This example shows how to specify the base-class constructor called when creating instances of a derived class.

public class BaseClass
{
    int num;

    public BaseClass()
    {
        Console.WriteLine("in BaseClass()");
    }

    public BaseClass(int i)
    {
        num = i;
        Console.WriteLine("in BaseClass(int i)");
    }

    public int GetNum()
    {
        return num;
    }
}

public class DerivedClass : BaseClass
{
    // This constructor will call BaseClass.BaseClass()
    public DerivedClass() : base()
    {
    }

    // This constructor will call BaseClass.BaseClass(int i)
    public DerivedClass(int i) : base(i)
    {
    }

    static void Main()
    {
        DerivedClass md = new DerivedClass();
        DerivedClass md1 = new DerivedClass(1);
    }
}
/*
Output:
in BaseClass()
in BaseClass(int i)
*/

C# 語言規格C# language specification

如需詳細資訊,請參閱<C# 語言規格>。For more information, see the C# Language Specification. 語言規格是 C# 語法及用法的限定來源。The language specification is the definitive source for C# syntax and usage.

另請參閱See also