简体   繁体   English

在C#中,你需要调用基础构造函数吗?

[英]In C#, do you need to call the base constructor?

In C#, if I have an inherited class with a default constructor, do I have to explicitly call the base class' constructor or will it be implicitly called? 在C#中,如果我有一个带有默认构造函数的继承类,我是否必须显式调用基类的构造函数或者它是否会被隐式调用?

class BaseClass
{
    public BaseClass()
    {
        // ... some code
    }
}
class MyClass : BaseClass
{
    public MyClass() // Do I need to put ": base()" here or is it implied?
    {
        // ... some code
    }
}

You do not need to explicitly call the base constructor, it will be implicitly called. 您不需要显式调用基础构造函数,它将被隐式调用。

Extend your example a little and create a Console Application and you can verify this behaviour for yourself: 稍微扩展您的示例并创建一个控制台应用程序,您可以自己验证此行为:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass foo = new MyClass();

            Console.ReadLine();
        }
    }

    class BaseClass
    {
        public BaseClass()
        {
            Console.WriteLine("BaseClass constructor called.");
        }
    }

    class MyClass : BaseClass
    {
        public MyClass()
        {
            Console.WriteLine("MyClass constructor called.");
        }
    }
}

It is implied, provided it is parameterless. 只要它是无参数的,它是隐含的。 This is because you need to implement constructors that take values , see the code below for an example: 这是因为您需要实现获取值的构造函数 ,请参阅下面的代码以获取示例:

public class SuperClassEmptyCtor
{
    public SuperClassEmptyCtor()
    {
        // Default Ctor
    }
}

public class SubClassA : SuperClassEmptyCtor
{
    // No Ctor's this is fine since we have
    // a default (empty ctor in the base)
}

public class SuperClassCtor
{
    public SuperClassCtor(string value)
    {
        // Default Ctor
    }
}

public class SubClassB : SuperClassCtor
{
    // This fails because we need to satisfy
    // the ctor for the base class.
}

public class SubClassC : SuperClassCtor
{
    public SubClassC(string value) : base(value)
    {
        // make it easy and pipe the params
        // straight to the base!
    }
}

It's implied for base parameterless constructors, but it is needed for defaults in the current class: 这是基本无参数构造函数的含义,但是当前类中的默认值需要它:

public class BaseClass {
    protected string X;

    public BaseClass() {
        this.X = "Foo";
    }
}

public class MyClass : BaseClass
{
    public MyClass() 
        // no ref to base needed
    {
        // initialise stuff
        this.X = "bar";
    }

    public MyClass(int param1, string param2)
        :this() // This is needed to hit the parameterless ..ctor
    {
         // this.X will be "bar"
    }

    public MyClass(string param1, int param2)
        // :base() // can be implied
    {
         // this.X will be "foo"
    }
}

这是隐含的。

A derived class is built upon the base class. 派生类是基于基类构建的。 If you think about it, the base object has to be instantiated in memory before the derived class can be appended to it. 如果你考虑一下,基础对象必须在内存中实例化,然后才能将派生类附加到它上面。 So the base object will be created on the way to creating the derived object. 因此,将在创建派生对象的方式上创建基础对象。 So no, you do not call the constructor. 所以不,你不要调用构造函数。

AFAIK,如果需要向其传递任何值,则只需要调用基础构造函数。

You don't need call the base constructor explicitly it will be implicitly called, but sometimes you need pass parameters to the constructor in that case you can do something like: 您不需要显式调用基本构造函数,它将被隐式调用,但有时您需要将参数传递给构造函数,在这种情况下,您可以执行以下操作:

using System;
namespace StackOverflow.Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            NewClass foo = new NewClass("parameter1","parameter2");
            Console.WriteLine(foo.GetUpperParameter());
            Console.ReadKey();
        }
    }

    interface IClass
    {
        string GetUpperParameter();
    }

    class BaseClass : IClass
    {
        private string parameter;
        public BaseClass (string someParameter)
        {
            this.parameter = someParameter;
        }

        public string GetUpperParameter()
        {
            return this.parameter.ToUpper();
        }
    }

    class NewClass : IClass
    {
        private BaseClass internalClass;
        private string newParameter;

        public NewClass (string someParameter, string newParameter)
        {
            this.internalClass = new BaseClass(someParameter);
            this.newParameter = newParameter;
        }

        public string GetUpperParameter()
        {
            return this.internalClass.GetUpperParameter() + this.newParameter.ToUpper();
        }
    }
}

Note: If someone knows a better solution please tells me. 注意:如果有人知道更好的解决方案,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VS2005中的C#:在继承的类中,您是否需要显式调用超级构造函数? - C# in VS2005: in an inherited class, do you need to explicitly call the super-constructor? (C#) 当你想创建一个继承类时,你如何处理带有构造函数的基类? - (C#) How do you deal with a Base class with a constructor, when you want to make an inheriting class? C#在自己的构造函数之后调用基类的构造函数? - C# Call base class' constructor after own constructor? 从基本构造函数C#调用基类重写方法 - Call base class override method from base constructor C# 如何以及何时在C#中调用基类构造函数 - How and when to call the base class constructor in C# C# - 让所有派生类都调用基类构造函数 - C# - Making all derived classes call the base class constructor 当我在 C# 中继承一个方法并调用继承的方法时。 我需要使用 base 还是 this - When I inherit a method in C# and call the inherited method. Do I need to use base or this 如何在C#中对继承的基本构造函数进行操作? - How do I do operations on an inherited base constructor in C#? C#入门:如何进行构造函数链接,覆盖和使用:this /:base? - Beginner to C#: How to do constructor chaining, overriding and using :this / :base? 在C#中,如果未定义默认值,为什么还要调用this()构造函数? - In C#, why would you call : this() constructor if no default was defined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM