Home > php教程 > php手册 > Dive deep into C# class methods

Dive deep into C# class methods

WBOY
Release: 2016-07-06 13:30:28
Original
1201 people have browsed it

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,

Constructor

example1:

<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>
Copy after login

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

  1. The method name is the same as the class name
  2. No return value type
  3. Mainly complete the initialization work of the object.

here:

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

No-argument constructor

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>
}
Copy after login

Constructor with parameters

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>
}
Copy after login

Implicit constructor

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

Method overloading

Overloading of constructor

example:

<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>
}
Copy after login

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

  1. Method names are the same
  2. Method parameter types are different or the number of parameters is different
  3. In the same class

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.

Method overloading example

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>
Copy after login

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,


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template