How can I call a base class's parameterized constructor from a derived class if the derived class has no parameterized constructor?

前端 未结 3 911
轻奢々
轻奢々 2020-12-21 06:58

I have a base class with two constructors: a default constructor and a parameterized constructor. Another class inherits from that base class and it only has a default const

3条回答
  •  礼貌的吻别
    2020-12-21 07:27

    Here you go:

    // Parent class
    class Parent
    {
        public Parent()
            {
            // Paremeterless constructor
            }
    
            public Parent(string a, int b)
            {
            // Paremterised constructor
            }       
    }
    
    
    // Child class       
    class Child : Parent
    {
        public Child()
                    :base("hi", 10)
            {
            // Parameterized constructor of the base class is invoked   
            }
    }
    

提交回复
热议问题