On overloading _java of Java method

Source: Internet
Author: User

The overload of a method is one in which multiple methods with the same name, but with different parameters, can be defined in a class, and when invoked, the corresponding method is selected according to the unused parameter table.

Like what

public class Test {
  void max (int a,int b) {
    System.out.println (a>b? a:b);
  }
   
  void Max (double a,double b) {
    System.out.println (a>b? a:b);
  }
   
  public static void Main (string[] args) {
    Test t = new Test ();
    T.max (3,4);
    T.max (3.0,4.4);
  }
 

The output is:

 4
 4.4

Construction methods can also overload

Let's look at one more example.

Class chongzai{public
  void A (int a);
   public void A (strting a);
   public void A (int a,int b);
}

As the above is a overload and overload to meet the conditions:
1. Must be the same class
2. Method name (also can be called function) the same
3. Parameter type is different or the number of parameters is not the same

At the same time I also give the LZ to speak about the role of overloading it or the above example to say

Chongzai  CZ =new Chongzai ();
CZ.A (1);          Call a (int a);
CZ.A ("Passing Parameters");   Call a (String a)
cz.a (1,2);         Call a (int a,int b)

We've already said which method to call, and the calling method is that the program determines which method is invoked according to the parameters you enter.

Overloaded features well, let's say you play the game. The number of people who can play a game is not sure how many persons it is. Then you can use the overload.

For example, a maximum of 3 people, you can define 3 parameters.

public void A (String a);
public void A (String a,string b);
public void A (String a,string b,string c);

Two people then you call the 2-parameter Method 3 people you're going to call the 3-parameter method and how to invoke the above has explained

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.