简体   繁体   English

c#如何通过派生类对象单独调用基类构造函数?

[英]How to call base class constructor alone through derived class object in c#?

I got the following question during an interview: "how can you call base class constructor (default constructor) alone from derived class object".我在一次采访中得到了以下问题:“如何从派生类对象单独调用基类构造函数(默认构造函数)”。 That object should not call derived class default constructor.该对象不应调用派生类的默认构造函数。 How is it possible?这怎么可能?

This is the code:这是代码:

class a
{
    public a()
    {
        MessageBox.Show("Base class called");
    }    
}
class b : a 
{
    public b()
    {
        MessageBox.Show("Derived class");
    }
}

I just want to display base class constructor without the one from derived class being invoked.我只想显示基类构造函数,而没有调用派生类的构造函数。 How can I achieve this?我怎样才能做到这一点?

The way the question is worded sounds like a riddle, but I'd assume it means 'how do you call the base class default constructor from a derived class's non-default constructor'.问题的措辞方式听起来像一个谜语,但我认为它的意思是“如何从派生类的非默认构造函数调用基类默认构造函数”。

This will implicitly happen anyway, but if you want to be explicit you're looking for the base keyword to specify which constructor to call in the base class:无论如何,这将隐式发生,但如果您想显式,您正在寻找base关键字来指定在基类中调用哪个构造函数:

public Derived(object param) : base()
{

}

The only way to not call the derived class's default constructor is to have a non-default constructor in the derived class and instantiate the object using that.不调用派生类的默认构造函数的唯一方法是在派生类中有一个非默认构造函数并使用它实例化对象。

See this fiddle for a demo containing both default and non-default constructors.有关包含默认和非默认构造函数的演示,请参阅此小提琴

Assuming you have the following classes:假设您有以下课程:

class Base 
{
    public Base() { }
}    

class Derived : Base
{
    public Derived() { }   
}

The base -class´-constructor is automatically invoked when creating the derived one.创建派生类时会自动调用base类的构造函数。 Therefor it is not possible at all to call the base-class constructor without calling the derived-class one.因此,根本不可能在不调用派生类的情况下调用基类构造函数。 Only way is create an instance of type Base instead of Derived , if you create a Derived its constructor surely is called.唯一的方法是创建一个Base类型的实例而不是Derived ,如果你创建一个Derived它的构造函数肯定会被调用。

However if you have a second constrcutor within Derived which directly calls the Base -one you may of course bypass the defaulöt-constructor of your derived class:但是,如果您在Derived有第二个构造函数直接调用Base -one,您当然可以绕过派生类的默认构造函数:

class Derived : Base
{
    public Derived() { }   
    public Derived(params object[] args) : base() { }   
}

Now you can make the following calls:现在您可以进行以下调用:

Derived d = new Derived(); // will call both default-constructors
Derived d1 = new Derived(null); // calls the params-overloaded constructor and not its default one

Try this code::试试这个代码::

class Base 
{
    public Base() {
        Console.WriteLine("BaseClass Const.");
    }
}    

class Derived : Base
{
    public Derived() { }   
    public Derived(params object[] args) : base() { } 
}

    public class Program
    {
        public static void Main(string[] args)
        {

            Derived d = new Derived(null) ;

        }
    }

Expected output :预期输出:

BaseClass Const.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM