The constructor C # class analysis

Constructor

A constructor is a method, given the name of its type. Its method signature contains only the name of the method and its argument list; it does not include the return type.

Define rules constructor:
(1) the name of the constructor C # language must be the same as the class name.
(2) The constructor has no return value. It can take parameters, can be no arguments.
(3) an object class declaration, the system automatically call the constructor. In the constructor do not do the right thing than instance of the class initialization can not be explicitly called.
(4) can be overloaded constructors, methods to provide different class object initialization;
(5) If the constructor defined in declaration, the system will automatically generate a default constructor, this case body of the constructor is empty.
(6) Static constructors, with static modification, for initialization, a class has a constructor that allows only loaded when the class is instantiated, then modifiers public, private useless.
(7) constructor can use public, protected, private modifier. Generally, the type of the constructor is always public. indicate the type of private constructor class can not be instantiated, typically containing only a class with static members.
(8) () when referencing the parent structure: base () method, using the reference configuration itself overloaded (): this (int para) .
(8) a return type constructor is an instance of this class, and returns a normal type of process may be of any type.

1      // parent class 
2      public  class the Person
 . 3      {
 . 4          Private  String Last;
 . 5          Private  String First;
 . 6  
. 7          // constructor of the superclass 
. 8          public the Person ( String lastName, String firstName)
 . 9          {
 10              Last = lastName;
 . 11              First = firstName;
 12 is              the System.Console.WriteLine ( " parent class constructor parameters " );
 13          }
14      }
 15  
16      // subclass 
. 17      public  class Adult: the Person
 18 is      {
 . 19          // constructor arguments 
20 is          public Adult ( String firstName, String lastName): Base (lastName, firstName)
 21 is          { 
 22 is              the System.Console.WriteLine ( " subclass constructor parameters " );
 23          }
 24  
25          // static constructor (static constructor must be free parameters, has allowed access modifiers) 
26 is          static Adult ()
 27          {
28              the System.Console.WriteLine ( " subclass static constructor " );
 29          }
 30      }

transfer:

FIG calling sequence as follows:

 

In a class of access, the system will be the first implementation of the constructor statement. The constructor function is to create an object, the object's state to legalize. Before returning from the constructor, the object is uncertain and can not be used to perform any operation; constructor executed only after completion of the memory block to store object stored in only this one instance of the class. 

 

Part excerpt: https://www.cnblogs.com/tanding/archive/2012/06/26/2563501.html

 

Guess you like

Origin www.cnblogs.com/YourDirection/p/12341062.html