1. Demonstrate the inheritance of Constructors
- Declare an empty constructor to Prevent Automatic Generation of default constructor.
- If you do not use an access modifier for the constructor, it is still a private constructor by default.
- The private modifier is explicitly used to explicitly indicate that the class cannot be instantiated.
2. Static Constructor
- Static constructors have the following features:
- The static constructor neither has an access modifier nor a parameter.
- Before creating the first instance or referencing any static member, the class is automatically initialized by calling the static constructor.
- Static constructor cannot be called directly.
- InProgramThe user cannot control when to execute the static constructor.
- A typical use of a static constructor is to use this constructor to write entries to a log file when the class uses a log file.
- Static constructors are not hostedCodeIt is also useful when creating a packaging class.
3. Example:
UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. LINQ;
UsingSystem. text;
// Description: test the constructor.
NamespaceNettest
{
Public Class Father
{
PublicFather ()
{
Console. Out. writeline ("Father say");
}
Public static father ()
{}
*/
PublicFather (StringS)
{
Console. Out. writeline (S +": Father say");
}
}
Public Class Son:Father
{
/*
// Call the class constructor of the parent constructor without Parameters
Public son ()
{
Console. Out. writeline ("son say ");
}
*/
PublicSon (StringS)
{
Console. Out. writeline ("Good son say :");
}
/* Base display calls to the parent class Constructor
Public son ()
: Base ("Jack ")
{
Console. Out. writeline ("son say ");
}
*/
/* The subclass implicitly calls the non-argument constructor of the parent class */
/* This calls its own overloaded constructor */
PublicSon ()
:This("Call myself another constructor")
{
Console. Out. writeline ("Above sentence calling myself");
}
}
Public Class Testconstructor
{
Public VoidTest ()
{
New Son();
}
}
}