Fun to overload Constructors

Source: Internet
Author: User

Constructor
When the new keyword is used to create an instance of the class, the constructor of the class is used. A class can have multiple constructors that accept different parameter types at the same time, constructor is a special method. The constructor name is the same as the class name and has no return type.
No parameter constructor. If the class does not explicitly define the constructor, the compiler automatically provides constructors that do not accept parameters, therefore, a non-argument constructor is also called a default constructor (if you write a constructor with parameters, the compiler will no longer automatically provide the default constructor)

Method overload
In C #, two or more methods in the same class can have the same name, as long as their parameter declarations are different. In this case, this method is called overload. This process is called method overload, and method overload is a method for implementing polymorphism in C #.
When an overloaded method is called, C # uses the type or quantity of parameters to indicate the version of the actually called overloaded method. Therefore, the type or quantity of parameters for each overload method must be different. Although each overload method can have different return types, the return types are not enough to distinguish which method is used.
Methods With the same method name and parameter list and different return values are not allowed in C #.
In a class, the method name is the same and the parameter list is different (the number of parameters or the parameter type is different). This is called overloading. Note that the return value of the method is not mentioned here. That is to say, there are two conditions for determining whether a method constitutes a overload:
(1) In the same class;
(2) method names are the same;
(3) The parameter list is different.

/*The following content is reprinted and I think it is quite good.*/For example, the followingCode:

Public void show () // (1)

{

Console. writeline ("nothing ");

}

 

Public void show (INT number) // (2)

{

Console. writeline (number );

}

 

Public int show (INT number) // (3 ).

{

Console. writeline (number );

Return Number % 5;

}

For the above Code, (1) There is no parameter, (2) an int type parameter is used, and an overload is formed between (1) and (2. (2) Compared with (3), only the return value is different. Although the return value is not the same as the reload, methods with the same method name and parameter list and different return values are not allowed in C, therefore, (2) and (3) cannot exist in the code at the same time. (3) there is no way to compile the code without comments.

Next let's take a look at the complicated situation:

Class Program

{

Static void main (string [] ARGs)

{

String S = NULL;

Show (s );

Object o = "123 ";

Show (O );

}

Static void show (string S)

{

Console. writeline ("string ");

}

Static void show (Object O)

{

Console. writeline ("object ");

}

}

The running result is:

String

Object

We can draw two conclusions from the results:

(1) From string S = NULL; show (s); finally, the static void show (string s) method is called, in C #, method calls are exactly matched, that is, S is of the string type. Although the string type inherits from the object type, static void show (Object O) meets the conditions, however, in the method declaration, the static void show (string s) statement is the closest to the S type (because s is of the string type and is the closest to it ), therefore, execute the static void show (string s) instead of the static void show (Object O) method.
(2) From Object o = "123"; show (o); finally, we can call the static void show (Object O) method, if method overload exists in C #, the method of the object is called based on its refrence type (reference type) instead of the Instance type. Although "123" is of the string type, its refrence type is of the object type. Therefore, the static void show (Object O) method is called instead of the static void show (string s) method ).

Public class Program

{

Public static void main ()

{

Show (null );//It seems that null is a string.

Show ("");

Show (1 );

}

Static void show (Object O)

{

Console. writeline ("object ");

}

Static void show (string S)

{

Console. writeline ("string ");

}

}

The running result is:

String

String

Object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.