Chapter 4: Inherit the constructor of the-4.2.6 derived class

Source: Internet
Author: User

Note: the scope of this article is limited to the execution of constructors and does not involve the design of classes.

 

1. Call Sequence of Constructor

When instantiating a class, the System. Object is always called first, and then the class hierarchy is inherited from top to bottom until the class to be instantiated by the compiler.

For example:

Code: execution sequence of the constructor when instantiating a derived class

 Using System;

Namespace Constructor
{
Class Program
{
Static void Main (string [] args)
{
Bueaty fang = new Bueaty ();
Console. Read ();
}
}

Class Human // base class
{
Public Human () // base class Constructor
{
Console. WriteLine ("base ....");
}
}

Class Bueaty: Human // derived class
{
Public Bueaty () // constructor of the derived class
{
Console. WriteLine ("inherit ..");
}
}
}

The running result is:

Base ....
Inherit ..

Of course, the Object class constructor is executed before the Human class constructor is executed, but it cannot be displayed here.

 

2. You can call the constructors of a base class explicitly before executing a constructor, as shown in the constructor of the Bueaty class:

  Public Bueaty () // constructor of the derived class
: Base ()
{
Console. WriteLine ("inherit ..");
}

If the compiler does not find a call to another constructor before the constructor of the current class, the base class constructor will be called by default.

Besides calling the constructor of the base class with ": base ()", you can also use ": this ()" to call the overloaded constructor of the current class, both can contain parameters.

 

 

3. if a class defines a constructor with parameters, no default constructor is generated (the default constructor has no parameters). Therefore, if the code is written as follows:

Code: errors caused by no default constructor in the base class

  Using System;

Namespace Constructor
{
Class Program
{
Static void Main (string [] args)
{
Bueaty fang = new Bueaty (20 );
Console. Read ();
}
}

Class Human // base class
{
Public Human (int height) // base class Constructor
{
Console. WriteLine ("base ....");
Console. WriteLine ("Height is" + height );
}
}

Class Bueaty: Human // derived class
{
Public Bueaty (int age) // constructor of the derived class
{
Console. WriteLine ("inherit ..");
Console. WriteLine ("Age is" + age );
}
}
}

 

 

VS reports an error:

Error 1 'constructor. human' does not contain a Constructor that takes '0' arguments D: \ Study \ constructor \ Constructor \ Program. cs 24 15 Constructor
Double-click error. highlighted is the constructor of the Bueaty class. Originally, the constructor of the Bueaty class did not call the constructor of its base class Human class, by default, the compiler calls the non-argument of the Human class.

The Human class has defined a parameter-based constructor public Human (int height ), in this way, the default no-argument constructor is no longer provided.

The derived class Bueaty cannot be instantiated.

The solution is to define a non-argument constructor in the Human class. This function does nothing:

Code: Add a constructor without Parameters

  Class Human // base class
{
Public Human () // No-argument constructor for the base class
{}
Public Human (int height) // The base class has a constructor.
{
Console. WriteLine ("base ....");
Console. WriteLine ("Height is" + height );
}
}

Running result:

Inherit ..
Age is 20

4. of course, it does not mean that the base class must add this non-argument constructor. For example, if a base class explicitly calls a constructor with a parameter, then the base class constructor without parameters will not be called.

For example:

Code: when the base class does not have a constructor without Parameters
  Using System;

Namespace Constructor
{
Class Program
{
Static void Main (string [] args)
{
Bueaty fang = new Bueaty (20,165); // pass two parameters to instantiate an object
Console. Read ();
}
}

Class Human // base class
{
Public Human (int height) // The base class has a constructor.
{
Console. WriteLine ("base ....");
Console. WriteLine ("Height is" + height );
}
}

Class Bueaty: Human // derived class
{
Public Bueaty (int age, int height) // constructor of the derived class
: Base (height) // pass a parameter to the base class constructor.
{
Console. WriteLine ("inherit ..");
Console. WriteLine ("Age is" + age );
}
}
}

Output result:

Base ....
Height is 165
Inherit ..
Age is 20

A person writes a blog at the company on weekends to fulfill his promise of at least one technical blog every day ......

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.