Well-off accompany you to learn Java--------overload

Source: Internet
Author: User

The overload of a method is to allow more than one method of the same name to exist in the same class, as long as they have a different number of arguments or types. In this case, the method is called overloaded, and this process is called the overloading of the method.

Example: Testjava4_11.java

01//The following procedure describes the overloaded operation of the method

public class Testjava4_11

03 {

public static void Main (string[] args)

05 {

Int_sum int;

-Double double_sum;

Int_sum = Add (3,5); Call the Add method with two parameters

The value of System.out.println ("Int_sum = Add (3,5) is:" +int_sum);

Ten int_sum = Add (3,5,6); Call the Add method with three parameters

One System.out.println (the value of "Int_sum = Add (3,5,6) is:" +int_sum);

Double_sum = Add (3.2,6.5); The value passed in is Doule type

The value of System.out.println ("Double_sum = Add (3.2,6.5) is:" +double_sum);

14}

public static int Add (int x,int y)

16 {

X+y return;

18}

public static int Add (int x,int y,int z)

20 {

return x+y+z;

22}

All public static double add (double x,double y)

24 {

return x+y;

26}

27}

Output Result:

The value of Int_sum = Add (3,5) is: 8

The value of Int_sum = Add (3,5,6) is: 14

The value of Double_sum = Add (3.2,6.5) is: 9.7

It can be found that the add is overloaded three times, but each overloaded method can accept the number of parameters and the type of different, I believe the reader should now be able to understand the concept of method overloading.

Well-off accompany you to learn Java--------overload

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.