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,则从 ClassB 访问 ClassA 的成员,而不考虑 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