Constructors are mainly used to assign initial values to objects when creating objects to initialize objects. It is always used with the new operator in the statement for creating an object. A A = new ();
The constructor has the same name as the class.But it is a function with all the features of the function, the same class can have multiple constructors with different parameters, that is, the function polymorphism.
Constructor is the first method to be executed when a class is instantiated. Through this feature, an initial value can be assigned to the object.
The constructor does not return values and cannot be modified with void. Only the access modifier is used.
Each class has a constructor. If the user-defined class does not explicitly define Any constructor, the compiler automatically generates a default constructor for this type, class. The system automatically creates a no-argument constructor for you.
Example: class test // test class
{
Public test ()
{
// Constructor, initialization object
}
}
Constructor for class initialization. For example, if you define a class
Class
The constructor is written as follows:
Public A () // parameters can be included in brackets
{
// The statement used for initialization (the default constructor is called without parameters)
}
// Note:A class can have multiple constructors, which can be distinguished based on the number of parameters or different parameter types.
Every time you use Class A, for example, a A = new A (); // If the previously defined constructor has a parameter, you must pass the Parameter
Constructor cannot be called directly. It is automatically called only when an object is created through the new operator. The general method is called when the program executes the object;Every time a class (new A () is instantiated, the constructor is called.
Although the constructor is not explicitly called, it is automatically called when an object is created. But it cannot be called. In some cases, it is necessary to display the call. As long as the parent class has a constructor with parameters, the sub-class must display the call to the constructor of the parent class, because the constructor of the subclass cannot find the constructor of the parent class during instantiation (when the parent class has its own non-argument constructor, The subclass does not need to display the call ).
Overload of constructor: the so-called overload is several methods with the same name in the parameter list. The overload of constructor is actually not much different from the overload of the method, except that the access modifier is public, no return value type is used.Below is a small example:
1 class animal 2 {3 static void main (string [] ARGs) 4 {5 // method 1 6 animal animal1 = new animal (); 7 animal1.name = "rabbit "; 8 animal1.color = "white"; 9 animal1.speed = 20; 10 animal1.run (); 11 // method 2 12 animal = new animal ("rabbit", "black "); 13 animal2.run (30); 14 // method 3 15 animal anim_3 = new animal ("dog", "brown", 50); 16 animal3.run (); 17 console. writeline (); 18} 19 // The constructor loads 20 public animal () 21 {22} 23 public animal (string name, string color) 24 {25 this. name = Name; 26 This. color = color; 27} 28 public animal (string name, string color, int speed) 29 {30 31 This. name = Name; 32 33 this. color = color; 34 35 This. speed = speed; 36}
37 // name 38 private string name; 39 Public String name40 {41 get {return name;} 42 set {name = value;} 43} 44 // color 45 private string color; 46 Public String color47 {48 get {return color;} 49 set {color = value;} 50} 51 // speed 52 private int speed; 53 public int speed54 {55 get {return speed;} 56 set {speed = value;} 57} 58 // different implementations of the overloaded run method of the method 59 Public void run () 60 {61 console. writeline ("one" + this. color + "+ this. name + "starting with" + this. speed + "km/h speed running \ n"); 62 63} 64 public void run (INT speed) 65 {66 67 console. writeline ("one" + this. color + "+ this. name + "running at" + speed + "km/h"); 68 69} 70
71}
The result is as follows: the two methods of the run method have the same implementation load.