Call Constructor Base after Code Execution

后端 未结 9 2088
攒了一身酷
攒了一身酷 2021-02-12 12:33

Let say we have Class A and Class B. ClassB extends Class A. (ClassB : ClassA)

Now let\'s say that whenever I instantiate ClassB, I\'d like to Run some Random code and o

9条回答
  •  别那么骄傲
    2021-02-12 13:14

    You can not call base constructor. But a different thing is that when you declare an object of derived class both constructor derived and base is called.

        class ClassA
    {
        public ClassA()
        {
            Console.WriteLine("Initialization");
        }  
    }
    
    class ClassB : ClassA
    {
        public ClassB() //: base() 
        {
            // Using :base() as commented above, I would execute ClassA ctor before                                                         //          Console.WriteLine as it is below this line... 
            Console.WriteLine("Before new");
            //base() //Calls ClassA constructor using inheritance
            //Run some more Codes here...
        }
    }
    void main(string[] args)
        {
          ClassB b = new ClassB();
    
        }
    

提交回复
热议问题