Will the base class constructor be automatically called?

后端 未结 6 1505
孤街浪徒
孤街浪徒 2020-11-30 20:36
class Person
{
    public int age;
    public Person()
    {
        age = 1;
    }
}

class Customer : Person
{
    public Customer()
    {
        age += 1;
    }
         


        
6条回答
  •  死守一世寂寞
    2020-11-30 21:07

    Yes, the base class constructor will be called automatically. You do not need to add an explicit call to base() when there is a constructor with no arguments.

    You can easily test this by printing out the age of the customer after construction (link to ideone with a demo).

提交回复
热议问题