【问题标题】:why do we need base() keyword为什么我们需要 base() 关键字
【发布时间】:2014-05-29 20:57:51
【问题描述】:

我正在阅读一些代码,但不明白 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();

    }
}

对比

带基的代码

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();

    }
}

【问题讨论】:

    标签: c# base


    【解决方案1】:

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

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

    现在你可以指定调用哪个基类构造函数了:

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

    此外,如果您的基类缺少默认构造函数(或者如果该构造函数无法访问,请执行它的保护级别),那么在子类中指定一个是必需的

    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");
        }
    }
    

    【讨论】:

      【解决方案2】:

      在您的示例中它并不重要,因为默认情况下 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) {}
      }
      

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

      【讨论】:

        【解决方案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);
            }
        }
        

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            【解决方案5】:

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

            【讨论】:

              【解决方案6】:

              在您的情况下,没有必要。这是一个解释为什么它很重要的例子

               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);
              
                      }
                  }
              

              【讨论】:

                【解决方案7】:

                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

                【讨论】:

                  【解决方案8】:

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

                  【讨论】:

                    猜你喜欢
                    • 2016-07-27
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-05-27
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-06-09
                    相关资源
                    最近更新 更多