Java method Overloading

Source: Internet
Author: User

Java method Overloading

Class ChongZai {

Public void a (int );

Public void a (Strting );

Public void a (int a, int B );

}

 

The above is an overload, and the overload must meet the following conditions:

1. Must beSame class

2.The method name (or function) is the same.

3.The parameter type is different or the number of parameters is different.

 

The role of overload:

Or the above example

ChongZai cz = new ChongZai ();

Cz. a (1); // call a (int );

Cz. a ("passed parameter"); // call a (String)

Cz. a (1, 2); // call a (int a, int B)

 

Which method is called, and the program determines which method to call based on the parameters you enter.

 

 

Reload functions:

 

For example, if you are playing a game, you may be able to have multiple people complete the game. If you are not sure that you are a few people, you can use the heavy load.

For example, you can define up to three parameters.

Public void a (String );

Public void a (String a, String B );

Public void a (String a, String B, String c );

 

For two people, you can call the two parameter methods. For three people, you can call the three parameter methods.

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.