Constructor example1: static void Main( string [] args){ SE engineer = new SE(); engineer.Age = 25; enginner.Name = Ai Biancheng; // Omit other attribute assignment operations Console.WriteLine( engineer.SayHi()); } We know that to use the attributes and methods of a class, we must first instantiate the class,
<span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span><span style="color: #000000"> [] args) { SE engineer</span>=<span style="color: #0000ff">new</span><span style="color: #000000"> SE(); engineer.Age</span>=<span style="color: #800080">25</span><span style="color: #000000">; enginner.Name</span>=<span style="color: #800000">"</span><span style="color: #800000">艾边成</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #008000">//</span><span style="color: #008000">省略其他属性赋值操作</span> <span style="color: #000000"> Console.WriteLine(engineer.SayHi()); }</span>
We know that to use the attributes and methods of a class, we must first instantiate the class. In instance 1, create an SE object through SE engineer=new SE();. This method of creating a class instance is called a constructor.
In Example 1, the constructor is called to create an SE object and assign values to its properties one by one. If no values are assigned, the system will assign default values to each field of the class.
As can be seen from Example 1, the constructor of a class is a special method in the class, which has the following characteristics
When we are doing development, we generally do not do things other than initializing instances of classes in the constructor, and do not try to call the constructor explicitly
Syntax:
<span style="color: #008000">//</span><span style="color: #008000">访问修饰符 类名()</span> <span style="color: #000000">{ </span><span style="color: #008000">//</span><span style="color: #008000">方法体</span> }
Syntax:
<span style="color: #008000">//</span><span style="color: #008000">访问修饰符 类名(参数列表)</span> <span style="color: #000000">{ </span><span style="color: #008000">//</span><span style="color: #008000">方法体</span> }
When we do not explicitly define a class constructor in the class, the system will automatically define a parameterless constructor without a method body for us. This is the implicit constructor. It is worth noting that when we When you explicitly define the constructor of a class, the system will not define the implicit constructor of the class for us
<span style="color: #000000">Public Class SE { </span><span style="color: #0000ff">string</span><span style="color: #000000"> id; </span><span style="color: #0000ff">string</span><span style="color: #000000"> name; </span><span style="color: #008000">//</span><span style="color: #008000">带参构造</span> <span style="color: #0000ff">public</span> SE(<span style="color: #0000ff">string</span> id,<span style="color: #0000ff">string</span><span style="color: #000000"> name) { </span><span style="color: #0000ff">this</span>.id=<span style="color: #000000">id; </span><span style="color: #0000ff">this</span>.name=<span style="color: #000000">name; } </span><span style="color: #008000">//</span><span style="color: #008000">无参构造</span> <span style="color: #0000ff">public</span><span style="color: #000000"> SE() { } </span><span style="color: #008000">//</span><span style="color: #008000">省略SE类的其它代码</span> }
It can be clearly seen from this code program that there are two constructors in the SE class with the same method name but different numbers of parameters. This method is method overloading.
From the above examples we can summarize the characteristics of method overloading
It should be noted that methods with the same method name and parameter class table have different return value types and cannot be called method overloading.
example:
Public <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span><span style="color: #000000"> [] args) { Console.WriteLine(</span><span style="color: #800080">8</span><span style="color: #000000">); Console.WriteLine(</span><span style="color: #800000">"</span><span style="color: #800000">Hello</span><span style="color: #800000">"</span><span style="color: #000000">); }</span>
In the example we can see that the first WriteLine method accepts an int type parameter, and the second WriteLine method accepts a string type parameter. WriteLine() provides a variety of overloaded methods to meet various needs,