Java must learn the method of the overload (overload) _java

Source: Internet
Author: User

Overload of a method

  

Method name, but the parameter is different, this is the overload (overload).

The so-called parameters are not the same, there are two main points: first, the number of parameters is not the same, the second is the type of parameter is not the same. As long as the two aspects of which are different on the one hand can constitute a method of overloading.

Package cn.galc.test;

public class Testoverload {

  void max (int a, int b) {
    System.out.println (a > B a:b);
  }
   /* int max (int a, int b) { 
   * return     a > b a:b; 
   *} */

  void Max (float A, float b) {
    System.out.println (a > B a:b);
  }


The two Max methods with the void modifier have the same names, but they have different parameter types, so they can form overloads. the int max (int a, int b) method and void Max (int a, int b) methods do not form overloads, now they are two methods with the same name, and the method of declaring two names in one class is not allowed, and compilation can be faulted. Method names, like parameter types, only the return value is not the same, this does not constitute an overload, because the method can call a method without using it to return a value, so when the two methods are called, the number of integers is passed in, since two methods are the same name, the same parameter type, So the compiler can't tell which method to call. The reason that makes the overload deeper: as long as the two method compilers can differentiate between, and when the call to know which one to call is not confusing, these two methods constitute overloading.

Look at the following two ways:

int max (int a, int b) {
  System.out.println ("called int max (int a, int b) method");
  Return a > B? a:b;
}
   
int Max (short A, short b) {
  System.out.println ("called int max (short A, short B) method");
  Return a > B? a:b;
}

These two methods can also be overloaded, because the compiler sees an integer as an int type. So when an integer is passed in, the compiler first calls this method of Max (int a, int b). and to call Max (short A, short b), you have to write this in the Main method:

public static void Main (string[] args) {
  testoverload t= new Testoverload ();
  T.max (3,4); This is called Max (int a, int b) This method short
  a = 3;
  Short B = 4;
  T.max (A, b); Here is the method of Max (short A, short b).
}

Ii. Overloading of construction methods

As with normal methods, the construction method can also overload

Package cn.galc.test;

public class Person {

  int id;
  int age;
  
  /**
   * Construction method */Public person
  () {
    id=0;
    age=20;
  }
  
  /**
   * Construction method overload one
   * @param i
   /public person
  (int i) {
    id=i;
    age=20;
  }
  
  /**
   * Construction method overload two
   * @param i
   * @param j */Public person
  (int i,int j) {
    id=i;
    Age=j
  }
}

The above is the Java method for the overload of the detailed explanation, I hope to help you learn.

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.