C # constructor (the constructor)

Defined constructor

Definition and role: constructor method is run automatically when constructing reference to the class. Therefore, each must have at least one class constructor.
Accessing a class, the compiler first performs the code in the constructor.
It is the class of the same name, arguments to the constructor parameters can be obtained, but with a different method is also a constructor while "Examples of type (i.e. calling must be carried out by way of example, reference may also be referred to as type)" but not return any value (even void).

Classification constructor

Can be divided according to the access modifiers
public constructor, private constructors, protected constructor

Access modifier is omitted, the default constructor is not private, and all variables the same method.
According to whether parameters
have parameters constructor and no argument constructor
according to whether the custom
default constructor and custom builder
default constructor is a public, non-parametric, compiler custom generated constructor, which is implicit present, if the constructor to create a custom, then the compiler does not automatically generate a default constructor
when calling the constructor, the compiler determines that the unique matching configuration according to the new operator specified parameter list

Guess you like

Origin www.cnblogs.com/lpxspring/p/12408229.html