为什么我们需要base()关键字

时间:2014-04-15 13:14:14

标签: c# base

我正在阅读一些代码,并且不了解基础工作。阅读一些例子,但他们没有解释我的问题。

两者都给出相同的输出。为什么我们需要base()?

没有基础的代码

class B : A
{
    public B()
    {
        Console.WriteLine("B");
    }
}
class A
{
    public A()
    {
        Console.WriteLine("A");
    }
}
class test
{
    static void Main()
    {
        A a = new A();
        B b = new B();
        Console.ReadLine();

    }
}

vs

基础代码

class B : A
{
    public B():base()
    {
        Console.WriteLine("B");
    }
}
class A
{
    public A()
    {
        Console.WriteLine("A");
    }
}
class test
{
    static void Main()
    {
        A a = new A();
        B b = new B();
        Console.ReadLine();

    }
}

8 个答案:

答案 0 :(得分:15)

在您的示例代码中,没有必要,因为如果您没有指定默认构造函数,编译器将隐式调用默认构造函数。但是,假设您在基类中有两个构造函数:

class A
{
    public A()
    {
        Console.WriteLine("A");
    }

    public A(string foo)
    {
        Console.WriteLine(foo);
    }
}

现在您可以指定要调用的基类构造函数:

class B : A
{
    public B() : base("Foo")
    {
        Console.WriteLine("B");
    }
}

此外,如果您的基类缺少默认构造函数(或者如果该构造函数不可访问它的保护级别),那么在子类中指定一个是 required

class A
{
    public A(string foo)
    {
        Console.WriteLine(foo);
    }
}

class B : A
{
    // Error: 'A' does not contain a constructor that takes 0 arguments
    public B()
    {
        Console.WriteLine("B");
    }
}

答案 1 :(得分:6)

在您的示例中,它并不重要,因为默认情况下C#将调用默认构造函数。这意味着以下内容基本相同。

class A
{
   public String MyProperty { get; set; }
}

class A 
{
   public A() {}
   public String MyProperty { get; set; }
}

在你的调用中,B的构造函数是不明智地调用它的基类A的构造函数。如果你要让A构造函数接受一个参数:

class A
{
   public A(String myStr) {}
}

然后您会发现您的代码不再编译。

答案 2 :(得分:3)

当您调用的基础对象上的构造函数接受参数时,

base非常有用。它允许您将参数带到子类构造函数并将它们传递给基础。

class B : A
{
    public B(string msg):base(msg)
    {
        Console.WriteLine("B" + msg);
    }
    public B():base("default message")
    {
        Console.WriteLine("B" + msg);
    }
}
class A
{
    public A(string msg)
    {
        Console.WriteLine("A" + msg);
    }
}

答案 3 :(得分:0)

base()是超类的默认构造函数。如果你定义了多个构造函数以及调用特定的基础构造函数,它会很有用。

答案 4 :(得分:0)

Base调用父ctor,它可以设置一些资源,这些资源对子类是隐藏的。在你的情况下,你有默认的构造函数,并且它们都将被调用,但是你可以在父类中创建带有参数的ctor,并从带有一些参数的child的默认ctor调用它。这在自定义例外中广泛使用,例如

答案 5 :(得分:0)

在你的情况下,它没有必要。这里有一个例子来解释它为什么重要

 class B : A
    {
        public B(int sharedvalue):base(sharedValue)
        {
            Console.WriteLine("B");
        }
    }
    class A
    {
     private  int _sharedValue;  
        public A(int sharedValue)
        {  // if you have a lot of check to do here it will be best to call the default constructor 
              _sharedValue = sharedValue; 
            Console.WriteLine("A");
        }
      property int SharedProp
    { get{return _sharedValue;} }

    }
    class test
    {
        static void Main()
        { 

            int val = 5 ; 

            A b = new B(val);
            Console.WriteLine(A.SharedProp);

        }
    }

答案 6 :(得分:0)

Base用于构造函数。派生类构造函数需要从其基类调用构造函数。当默认构造函数不存在时,可以引用自定义基础构造函数,以及base。

示例:使用基础构造函数初始化程序的程序:C#

using System;

public class A // This is the base class.
{
    public A(int value)
    {
    // Executes some code in the constructor.
    Console.WriteLine("Base constructor A()");
    }
}

public class B : A // This class derives from the previous class.
{
    public B(int value)
    : base(value)
    {
    // The base constructor is called first.
    // ... Then this code is executed.
    Console.WriteLine("Derived constructor B()");
    }
}

class Program
{
    static void Main()
    {
    // Create a new instance of class A, which is the base class.
    // ... Then create an instance of B, which executes the base constructor.
    A a = new A(0);
    B b = new B(1);
    }
}

输出

  

基础构造函数A()

     

基础构造函数A()

     

派生构造函数B()

您还可以阅读更多内容:hereherehere

答案 7 :(得分:0)

它用于调用父类构造函数!即使编译器将隐式调用父类的默认构造函数,但如果您有多个构造函数,则可以指定要从父类调用的构造函数的版本。 将调用父类构造函数,其签名将与base()匹配。

相关问题