Java object-oriented-method overloading

Source: Internet
Author: User

Java object-oriented-method overloading

The so-called method of overloading,

There are two or more methods of the same name in the class, but there is at least one difference in the number, type, and order of the methods, at which time the Bureau forms the overloaded method;

On the code:

1  Packagecom.java1234.chap03.sec03;2  3  Public classDemo01 {4  5     intAddintAintb) {6System.out.println ("Method One"));7         returnA +b;8     }9      Ten     /** One * method overloads, number of arguments are not the same A      * @parama -      * @paramb -      * @paramC the      * @return -      */ -     intAddintAintBintc) { -System.out.println ("Method Two"); +         returna+b+C; -     } +       A     /** at * Overloading of methods, different types of parameters -      * @parama -      * @paramb -      * @return -      */ -     intAddinta,string B) { inSystem.out.println ("Method three"); -         returnA +Integer.parseint (b); to     } +       -     /** the * The number of parameter types is the same, the return value type is not overloaded, direct error, said method duplicate name *      * @paramargs $      */Panax Notoginseng     /*long Add (int a,int b) { - return a+b; the     }*/ +       A      Public Static voidMain (string[] args) { theDemo01 demo=NewDemo01 (); +System.out.println (Demo.add (1, 2)); -System.out.println (Demo.add (1, 2,3)); $System.out.println (Demo.add (1, "3")); $     } -}

Operation Result:

Method One
3
Method Two
6
Method Three
4

Here's a note. If the number of parameters and type, the return value is not the same, can not be overloaded, is directly a compilation error, the compiler thinks the method is duplicated.

Specific reasons: we give the following example code:

1  Packagecom.java1234.chap03.sec03;2  3  Public classDemo01 {4  5     intAddintAintb) {6System.out.println ("Method One"));7         returnA +b;8     }9  Ten     /** One * The number of parameter types is the same, the return value type is not overloaded, direct error, said method duplicate name A      * @paramargs -      */ -     LongAddintAintb) { the         returnA +b; -     } -       -      Public Static voidMain (string[] args) { +Demo01 demo=NewDemo01 (); -         intM=demo.add (1, 2);//call a method that returns a value of type int +         LongN=demo.add (1, 2);//calling the return value is a long type method A           atDemo.add (1, 2);//If such a compiler fainted, would you like to call a method that returns a value of type int or long ? -           -     } -}

If the calling method specifies a return value type, we can determine if the call is a method, such as int m=demo.add, because the return value m is the int type that certainly

called int add (int a,int b) {method long N=demo.add (1, 2) because the return value n is a long type, that must be called long add (int A,int b) {method

However, if the direct demo.add (); The compiler is a SB, you TM is the method that calls the return value is the int type, or the return value is a long type method;

Java object-oriented-method overloading

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.