C #: Method overload, constructor, and overload constructor Summary

Source: Internet
Author: User


Method overload

The same function can be applied to various types of data. It declares more than two methods with the same name to achieve the same processing for different data types.

Method overload requirements

1. The names of the overloaded methods must be the same.

2. For an overloaded method, the number or type of parameters must be different.

 

For example, we have defined swap (ref int A, ref intb). This function is used to exchange the values of Two integer variables, but does not process floating point data. We are defining a SWAp (ref flot, ref flot B), so that the swap method can realize the exchange of integer variable values or floating point data exchange (the system will decide to call the appropriate method based on the data type)

 

Constructor

The main function is to initialize an object when creating an object (declaring an object. A class definition must have at least one constructor. If no constructor is declared when a class is defined, the system will provide a default constructor. For example, you may better understand it:

The result is:


If you want to set the object data member to a specified value when creating an object, you must specify the constructor.

 

Requirements for declaring constructors:

1. constructors cannot have return types

2. the constructor name must be the same as the class name.

 

The constructor is usually used to initialize data members when creating an object. Therefore, the constructor must use a form parameter.


Public student (string ID, int age)

{

Id = ID;

Age = age;

}

Because the above constructor includes parameters, the system does not provide default constructor. Therefore, when creating an object, the actual parameters must be given according to the declared constructor parameter requirements.

Student S1 = new student ("90090", 22 );

The new keyword is followed by a call to the constructor.

If the parameter names used to declare the constructor are the same as the class data member names, the class data member names used in the constructor must have this guide.

Public student (string ID, int age)

{

This. ID = ID;

This. Age = age;

}

The keyword "this" indicates the created object, which is the reference parameter of the object automatically passed to the constructor when the object is declared.

 

Overload Constructor

Constructors and methods can be reloaded. The main purpose of the overload constructor is to provide more flexibility for object creation and meet different needs during object creation.

In the above example, if you only want to change the age, the overload constructor student only needs to have a parameter age.

Virtual Method

Declare a derived class method with the same name as the base class

Public New Method Name (parameter list ){}

Declare Virtual Methods

Declaration format in base class

Publicvirtual method name (parameter list ){}

Declaration format in a derived class

Publicoverride method name (parameter list ){}

Call the base class Method

In a derived class, declare a method with the same name as a base class, also called a method overload. After a derived class reloads the base class method, if you call a method of the same name as the base class, use the base keyword.

 

Declare abstract classes and abstract methods

Public abstractclasse Class Name

{Public abstract return type method name (parameter list );}

Overload abstract Method

Public override return type method name (parameter list)

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.