[英]why do we need base() keyword
我正在阅读一些代码而不了解基础工作。 阅读一些例子,但他们没有解释我的问题。
两者都给出相同的输出。 为什么我们需要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();
}
}
在您的示例代码中,没有必要,因为如果您没有指定一个,编译器将隐式调用默认构造函数。 但是,假设您在基类中有两个构造函数:
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");
}
}
在您的示例中,它并不重要,因为默认情况下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) {}
}
然后你会发现你的代码不再编译。
当您调用的基础对象上的构造函数接受参数时, 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);
}
}
base()是超类的默认构造函数。 如果您定义了多个构造函数以及调用特定的基础构造函数,那么它可能很有用。
Base调用父ctor,它可以设置一些资源,这些资源对子类是隐藏的。 在你的情况下,你有默认的构造函数,并且它们都将被调用,但是你可以在父类中创建带有参数的ctor,并从带有一些参数的child的默认ctor调用它。 例如,这广泛用于自定义例外
在你的情况下,没有必要。 这里有一个例子来解释为什么它很重要
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);
}
}
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()
它用于调用父类构造函数! 即使编译器将隐式调用父类的默认构造函数,但如果您有多个构造函数,则可以指定要从父类调用的构造函数的版本。 将调用父类构造函数,其签名将与base()匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.