I. C # constructor? Construct, Function
Constructor is a special member function. It is mainly used to allocate storage space for objects and initialize data members.
Constructor has some special properties:
(1) The name of the constructor must be the same as that of the class;
(2) The constructor does not return a type. It can contain parameters or not;
(3) When declaring a class object, the system automatically calls the constructor. The constructor cannot be explicitly called;
(4) constructors can be overloaded to provide different methods for initializing class objects;
(5) If no constructor is defined during declaration, the system automatically generates the default constructor. The constructor body is empty.
(6) static constructor. static modifier is used to initialize static variables. A class only allows one constructor to be loaded during class instantiation. The modifiers public and private are ineffective.
(7) public, protected, and private modifiers can be used ..
(8) use the (): base () method when referencing the parent class constructor. Use (): this (int para) to reference its own overloaded constructor ).
(7) public, protected, and private modifiers can be used.
Note:
1. After defining a constructor with parameters, it is best to define a non-argument constructor to avoid errors during subclass calls.
2. Call the base class constructor: base () or: base (Parameter List) to call other constructor of this class: this () or: this (parameter list)
3. The program does not call constructors without parameters by default only when base is used to display and call the constructor of the parent class.
2. What is the structure hierarchy or execution sequence of the C # constructor? Layer, Transfer, Execute
Constructs an object from the base class.
Public class MyBaseClass
{
Public MyBaseClass ()
{
Console. WriteLine ("In MyBaseClass ()");
}
Public MyBaseClass (int I)
{
Console. WriteLine ("In MyBaseClass (int I )");
}
}
Public class MyDerivedClass: MyBaseClass
{
Public MyDerivedClass ()
{
Console. WriteLine ("In MyDerivedClass ()");
}
Public MyDerivedClass (int I)
{
Console. WriteLine ("In MyDerivedClass (int I )");
}
// Public MyDerivedClass (int I, int j)
//{
// Console. WriteLine ("In MyDerivedClass (int I, int j )");
//}
Public MyDerivedClass (int I, int j)
: Base (I)
{
Console. WriteLine ("In MyDerivedClass (int I, int j): base (I )");
}
}
Class Program
{
Static void Main (string [] args)
{
// Event1
MyDerivedClass myObj1 = new MyDerivedClass ();
Console. WriteLine ();
// Event2
MyDerivedClass myObj2 = new MyDerivedClass (4 );
Console. WriteLine ();
// Event3
MyDerivedClass myObj3 = new MyDerivedClass (4, 8 );
Console. WriteLine ();
Console. ReadKey ();
}
}
Program output:
In mybaseclass ()
In myderivedclass ()
In mybaseclass ()
In myderivedclass (int I)
In mybaseclass (int I)
In myderivedclass (int I, Int J): Base (I)
Obviously, the program will not call constructors without parameters by default only when the base display calls the constructor of the parent class.
Http://junmail.javaeye.com/blog/197089