An understanding of the basic class constructor called by a derived class in C #

Source: Internet
Author: User

The default constructor in this article refers to the system default non-argument constructor without compiling the 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

This article from CSDN blog, http://blog.csdn.net/cj205/archive/2008/12/05/3449828.aspx

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.