1: Accessibility of the construction method
The construction method can be either common or private.
2: Default constructor method
The default constructor method is a constructor without parameters.
If no constructor method is added for the class, a hidden default construction method is automatically generated, but the default construction method is not automatically generated if the user adds any of the constructor methods. That is, if the user adds their own defined construction method to the class, the object can only be generated using the user-defined constructor methods.
3: Constructor method call construction method
The format is as follows:
Constructor method Name (parameter list): This (parameter list)
{
Method body
}
Example:
Class derivedclass{Public DerivedClass (): this (+) {Console.WriteLine ("Derived class ' s Default constructo R "); } public derivedclass (int i) {Console.WriteLine ("Derived Class ' s Constructor with Param: {0}", i); }}
And the difference between Java
In Java, the constructor method calls another constructor method that uses this (parameter), but Java is placing it as a statement in the method body, and the This statement must also be the first statement of the method body.
4: In the inheritance structure, the generation of the parent class object
As with C + +, Java, if there is an inheritance structure, then the child class object will be generated as a parent class object.
By default, the default construction method of the parent class is automatically called when the subclass is constructed (all constructor methods) to produce the parent class object, so if there is no special description in the constructor method of the subclass, and the parent class does not have a default constructor, the system prompts an error.
In general, we will explicitly tell the system in the constructor method of the subclass, which constructor method of the subclass to invoke which constructor of the parent class.
The format is as follows:
Constructor method name for subclass: base (parameter list)
{
Method body
}
This allows the system to invoke the corresponding construction method of the parent class before executing this constructor method.
The difference from Java: the keyword used in Java is super, and is also placed at the beginning of the method body as a statement.
Class 02: Constructing Objects in C #