1. Constructors
A constructor is a special method of a class that never returns a value (even a void), and the method name and class name are the same, supporting overloading as well.The constructor is called indirectly when an object is created using the new keyword to initialize the values of fields and properties for the object.
A parameterless constructor is the default constructor. When we do not create any constructors, the compiler creates a default constructor for us. Conversely, when we create any constructors, the compiler does not create default constructors for us.
Next, we design an air conditioning class without a constructor, an air conditioning class without a default constructor, and an air conditioning class with multiple constructors, and instantiate them.
1 /// <summary> 2 /// Air conditioner Air 3 /// </summary> 4 public class Air 5 { 6 #region Constructor 7 //Structure of default constructors 8 //public Air() { } 9 #endregion 10 11 #region field 12 /// <summary> 13 /// Air conditioning temperature 14 /// </summary> 15 public int temperature; 16 17 /// <summary> 18 /// Up and down direction of air conditioning (with int Quantitative Direction, 0 for lower, 1 for lower, 2 for upper, 3 for upper, 4 for upper) 19 /// </summary> 20 public int verticalDirection; 21 22 /// <summary> 23 /// Air conditioning switch (with int Quantization switch, 0 for off, 1 for on) 24 /// </summary> 25 public int switch_C; 26 #endregion 27 } 28 /// <summary> 29 /// Air conditioner AirOther 30 /// </summary> 31 public class AirOther 32 { 33 #region Constructor 34 /// <summary> 35 /// A parameter constructor 36 /// </summary> 37 /// <param name="num"></param> 38 public AirOther(int num) 39 { 40 //verticalDirection and switch_C Will be set to the default value of 0. 41 temperature = num; 42 } 43 44 /// <summary> 45 /// Multiparameter constructor 46 /// </summary> 47 /// <param name="openOrClose">0 On behalf of Close, on behalf of 1</param> 48 /// <param name="upOrdown">0 Representatives, 1 for lower, 2 for upper, 3 for upper, 4 for upper</param> 49 /// <param name="num">Air conditioning temperature</param> 50 public AirOther(int openOrClose, int upOrdown, int num) 51 { 52 temperature = num; 53 verticalDirection = upOrdown; 54 switch_C = openOrClose; 55 } 56 #endregion 57 58 #region field 59 /// <summary> 60 /// Air conditioning temperature 61 /// </summary> 62 public int temperature; 63 64 /// <summary> 65 /// Up and down direction of air conditioning (with int Quantitative Direction, 0 for lower, 1 for lower, 2 for upper, 3 for upper, 4 for upper) 66 /// </summary> 67 public int verticalDirection; 68 69 /// <summary> 70 /// Air conditioning switch (with int Quantization switch, 0 for off, 1 for on) 71 /// </summary> 72 public int switch_C; 73 #endregion 74 } 75 /// <summary> 76 /// Air conditioner AirConditioner 77 /// </summary> 78 public class AirConditioner 79 { 80 #region Constructor 81 /// <summary> 82 /// non-parameter constructor 83 /// </summary> 84 public AirConditioner() 85 { 86 temperature = 28; 87 verticalDirection = 3; 88 switch_C = 0; 89 } 90 91 /// <summary> 92 /// A parameter constructor 93 /// </summary> 94 /// <param name="num"></param> 95 public AirConditioner(int num) 96 { 97 //verticalDirection and switch_C Will be set to the default value of 0. 98 temperature = num; 99 } 100 101 /// <summary> 102 /// Multiparameter constructor 103 /// </summary> 104 /// <param name="openOrClose">0 On behalf of Close, on behalf of 1</param> 105 /// <param name="upOrdown">0 Representatives, 1 for lower, 2 for upper, 3 for upper, 4 for upper</param> 106 /// <param name="num">Air conditioning temperature</param> 107 public AirConditioner(int openOrClose, int upOrdown, int num) 108 { 109 temperature = num; 110 verticalDirection = upOrdown; 111 switch_C = openOrClose; 112 } 113 #endregion 114 115 #region field 116 /// <summary> 117 /// Air conditioning temperature 118 /// </summary> 119 public int temperature; 120 121 /// <summary> 122 /// Up and down direction of air conditioning (with int Quantitative Direction, 0 for lower, 1 for lower, 2 for upper, 3 for upper, 4 for upper) 123 /// </summary> 124 public int verticalDirection; 125 126 /// <summary> 127 /// Air conditioning switch (with int Quantization switch, 0 for off, 1 for on) 128 /// </summary> 129 public int switch_C; 130 #endregion 131 } 132 /// <summary> 133 /// Implement Object 134 /// </summary> 135 public class RealizeObject 136 { 137 public Realize() 138 { 139 //Air Class has no constructor, create airNotConstructor Object calls the default constructor for all of its int Type field is assigned a default value of 0. 140 Air airNotConstructor = new Air(); 141 142 //AirOther Class cannot be used new AirOther()Create objects because when we create any constructors, the compiler does not create default constructors for us, so if you want to use default constructors, you should show Create. 143 //AirOther airNotDefaultConstructor = new AirOther(); 144 145 //Creating object air In the process, the call is AirConditioner Class's parameterless constructor, which is air The field of the assignment code is as follows: 146 //air.temperature = 28; 147 //air.verticalDirection = 3; 148 //air.switch_C = 0; 149 AirConditioner air = new AirConditioner(); 150 151 //Creating object airOne In the process, the call is AirConditioner A parameter constructor for a class that is an object temperature Assignment 21, two others int Type field is assigned a default value of 0. 152 AirConditioner airOne = new AirConditioner(21); 153 154 //Creating object airOpen18 In the process, the call is AirConditioner Class's multi-parameter constructor and assigns the values passed in by the corresponding fields. 155 AirConditioner airOpen18 = new AirConditioner(1, 1, 18); 156 } 157 }
The default constructor assigns default values to data types as follows:
The bool type is set to false,
Set the numeric type to 0 or 0.0,
The char type is set to a single empty character.
The DataTime type is set to 1/1/0001 12:00:00 AM,
Object references (including string s) are set to null.
2. this keyword
One of the functions of this keyword is to represent the current class. When the parameter name of a method is the same as the field or property name of a class, the field or property name of the class is represented by this.XXX, which distinguishes it from the parameter name of the method.The second purpose of this keyword is to simplify redundant logic code in multiple constructors.
1 /// <summary> 2 /// Classes of washing machines 3 /// </summary> 4 public class WashingMachine 5 { 6 /// <summary> 7 /// Duration (in minutes) 8 /// </summary> 9 public int duration; 10 11 /// <summary> 12 /// Total number of garments 13 /// </summary> 14 public int clothesNum; 15 16 public WashingMachine() { } 17 18 public WashingMachine(int duration) 19 { 20 if (duration > 60) 21 { 22 duration = 60; 23 } 24 //adopt this Keyword distinguishes the field name of a class from the parameter name of a constructor 25 this.duration = duration; 26 } 27 28 public WashingMachine(int duration, int clothesNum) 29 { 30 if (duration > 60) 31 { 32 duration = 60; 33 } 34 //adopt this Keyword distinguishes the field name of a class from the parameter name of a constructor 35 this.duration = duration; 36 this.clothesNum = clothesNum; 37 } 38 }
When a constructor is used to assign an initial value to the washing machine length, more than 60 minutes equals 60 minutes uniformly, in which case multiple constructor logic codes are redundant.By means of a constructor chain, the constructor with the most parameters is the primary function, and other constructors initialize the data with the constructor with the most parameters.
1 /// <summary> 2 /// Classes of washing machines 3 /// </summary> 4 public class WashingMachine 5 { 6 /// <summary> 7 /// Duration (in minutes) 8 /// </summary> 9 public int duration; 10 11 /// <summary> 12 /// Total number of garments 13 /// </summary> 14 public int clothesNum; 15 16 public WashingMachine() { } 17 18 public WashingMachine(int duration) : this(duration, 0) { } 19 20 public WashingMachine(int clothesNum) : this(0, clothesNum) { } 21 22 public WashingMachine(int duration, int clothesNum) 23 { 24 if (duration > 60) 25 { 26 duration = 60; 27 } 28 //adopt this Keyword distinguishes the field name of a class from the parameter name of a constructor 29 this.duration = duration; 30 this.clothesNum = clothesNum; 31 } 32 }
Note: When the constructor WashingMachine(int duration) is called, it enters the main constructor first, and when the main constructor is executed, it returns to the WashingMachine(int duration) constructor to execute its code.