Invoking constructor of derived class execute before constructor of base class

后端 未结 3 949
无人共我
无人共我 2021-01-03 04:20

Well, originally I had a couple of constants (like MAX_SPEED) with different values in every of the derived classes. The idea was to use those values in some methods of the

相关标签:
3条回答
  • 2021-01-03 04:48

    One more option is to make child class constructor as static , so that it executes first than parent class constructor. but it not preferable it violates the oop design

    0 讨论(0)
  • 2021-01-03 04:49

    No. The base class constructor is always executed before the body of the derived class constructor. However:

    • Any instance variable initializers in the derived class are executed before the base class constructor
    • The base class constructor can execute virtual methods which can be overridden in the derived class. This is almost always a bad idea though. (All kinds of normal preconditions are invalid at this point. You can observe readonly variables which haven't been set yet because they'll be set in the constructor body, for example. Ick.)

    To demonstrate both of these:

    using System;
    
    class BaseClass
    {
        public BaseClass()
        {
            VirtualMethod();
            Console.WriteLine("BaseClass ctor body");
        }
    
        public virtual void VirtualMethod()
        {
            Console.WriteLine("BaseClass.VirtualMethod");
        }
    }
    
    class DerivedClass : BaseClass
    {
        int ignored = ExecuteSomeCode();
    
        public DerivedClass() : base()
        {
            Console.WriteLine("DerivedClass ctor body");
        }
    
        static int ExecuteSomeCode()
        {
            Console.WriteLine("Method called from initializer");
            return 5;
        }
    
        public override void VirtualMethod()
        {
            Console.WriteLine("DerivedClass.VirtualMethod");
        }
    }
    
    class Test
    {
        static void Main()
        {
            new DerivedClass();
        }
    }
    

    Output:

    Method called from initializer
    DerivedClass.VirtualMethod
    BaseClass ctor body
    DerivedClass ctor body
    

    Additionally, if your base class constructor takes a parameter, then you can execute some code in the derived class in order to provide an argument:

    DerivedClass() : base(SomeStaticMethod())
    

    All of these are fairly smelly though. What's your specific situation?

    0 讨论(0)
  • 2021-01-03 05:08

    No, you can't do that. Base classes are always initialized first. However, you can do something like this:

    class BaseClass
    {
        public BaseClass()
        {
            this.Initialize();
        }
    
        protected virtual void Initialize()
        {
            System.Console.WriteLine("This should be shown after");
        }
    }
    
    class DerivedClass : BaseClass
    {
        public DerivedClass() : base()
        {
        }
    
        protected override void Initialize()
        {
            System.Console.WriteLine("This should be shown first");
            base.Initialize();
        }
    }
    
    0 讨论(0)
提交回复
热议问题