To understand how a derived class in C # calls a base class Constructor

Source: Internet
Author: User

An understanding of the basic class constructor called by a derived class in C.
C # class default constructor in this document refers to the system default non-argument constructor without compiling Constructor

 

1. When the base class does not write its own constructor, the derived class calls the default constructor of the base class by default.

Ex:

Public class mybaseclass
{
}
 

Public class myderivedclass: mybaseclass
{
Public myderivedclass ()
{
Console. writeline ("I Am a subclass without parameters constructor ");
}
 

Public myderivedclass (int I)
{
Console. writeline ("constructor with a parameter ");
}
 

Public myderivedclass (int I, Int J)
{
Console. writeline ("constructor with two parameters in my subclass ");
}
}
When instantiating a derived class, the default constructor of the base class is called.
2. When a constructor is written in the base class, if the derived class does not specify the constructor to call, the constructor without parameters will be searched. If no constructor exists, an error is returned, in addition, no matter which constructor In the derived class is called, it is used to find the base class constructor without parameters, rather than parameter matching.

Ex:

Public class mybaseclass
{
Public mybaseclass (int I)
{
Console. writeline ("I Am a base class constructor with a parameter ");
}
}
 

Public class myderivedclass: mybaseclass
{
Public myderivedclass ()
{
Console. writeline ("I Am a subclass without parameters constructor ");
}
 

Public myderivedclass (int I)
{
Console. writeline ("constructor with a parameter ");
}
 

Public myderivedclass (int I, Int J)
{
Console. writeline ("constructor with two parameters in my subclass ");
}
}
An error is reported when the derived class is instantiated.
3. If constructors are written in the base class, you can specify a base class constructor In the derived class and use the base keyword.

Ex

Public class mybaseclass
{
Public mybaseclass (int I)
{
Console. writeline ("I Am a base class constructor with a parameter ");
}
}
 

Public class myderivedclass: mybaseclass
{
Public myderivedclass (): Base (I)
{
Console. writeline ("I Am a subclass without parameters constructor ");
}
 

Public myderivedclass (int I): Base (I)
{
Console. writeline ("constructor with a parameter ");
}
 

Public myderivedclass (int I, Int J): Base (I)
{
Console. writeline ("constructor with two parameters in my subclass ");
}
}
When the constructor with a parameter is used to instantiate a derived class, no error is reported because it specifies the constructor of the base class.
4. If the constructor in the base class does not contain any constructor, the constructor In the derived class must specify all the called base class constructor; otherwise, an error occurs.

Ex

Public class mybaseclass
{
Public mybaseclass (int I)
{
Console. writeline ("I Am a base class constructor with a parameter ");
}
}
 

Public class myderivedclass: mybaseclass
{
Public myderivedclass ()
{
Console. writeline ("I Am a subclass without parameters constructor ");
}
 

Public myderivedclass (int I): Base (I)
{
Console. writeline ("constructor with a parameter ");
}
 

Public myderivedclass (int I, Int J)
{
Console. writeline ("constructor with two parameters in my subclass ");
}
At this time, compilation will fail

To understand how a derived class in C # calls a base class Constructor

Related Article

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.