Java learning notes (method overloading)

Source: Internet
Author: User
static void f3(short x) { prt("f3(short)"); } static void f3(int x) { prt("f3(int)"); } static void f3(long x) { prt("f3(long)"); } static void f3(float x) { prt("f3(float)"); } static void f3(double x) { prt("f3(double)");} static void prt(String s) {   System.out.println(s); }

Method overloading (some translate to overload) means that the method name is the same, and the parameter sequence of the method is different (here it does not refer to rewriting in the inheritance relationship ):
1) only the return types are different and cannot be overloaded. This is stipulated in general programming languages. The reason is very simple. The method to be called is determined based on the return type,
It needs to be determined based on the recipient type of the returned value, which is very difficult in many cases (for example, the accept type is object, or the return value does not need to be received ).
2) different parameter sequences mainly refer to different parameter type sequences, rather than different parameter names;
3) when determining whether the parameter type is different, the inheritance relationship is not considered. Although classa is the parent class of classb, the following:
Public String dosomething (classa A) and Public String dosomething (classb B) are reloaded.

Pay attention to the following for heavy load:
1) for heavy loads of the main type (basic type), if the real parameter is a variable or a constant of the specified type (2.0d), or the constant type is uniquely identified (character, string, boolean ),
The method to be called is selected based on the variable type, but the real parameter is a constant and is not easy to determine the numeric type. In the absence of a clear type mark, integers are generally converted to the int type,
Generally, decimal places are converted to double. The test is as follows:

 

// Call the test:

Char A = 'a'; F3 (a); F3 (1); // F3 (10000000000); error F3 (0000000000l); F3 (2.0); F3 (2.0f ); f3 (2.0d );

Test results:

F3 (INT)
F3 (INT)
F3 (long)
F3 (double)
F3 (float)
F3 (double)
2) for heavy loads of inherited types, the call method is determined based on the variable type during the call, except that if the current variable type does not exist, the direction of the parent type is sought.
Search up until it is found (it will certainly be found. If it cannot be found, an error will be reported ):

The following myclassa-> D is the parent-child relationship in sequence, and the following is the overload method:

public class OverloadClass {public void DoSomething(MyClassA a){System.out.println("MyClassA a:"+a.getClass().getName());}    public void DoSomething(MyClassB b)    {    System.out.println("MyClassB b:"+b.getClass().getName());    }    public void DoSomething2(MyClassA a)    {    System.out.println("MyClassA a:"+a.getClass().getName());    }    public void DoSomething2(MyClassC c)    {    System.out.println("MyClassC c:"+c.getClass().getName());    }    public void DoSomething3(MyClassC c)    {    System.out.println("MyClassC c:"+c.getClass().getName());    }}

Reload test code:

Overloadclass theoc = new overloadclass (); myclassa Thea = new myclassa (); myclassb theb = new myclassb (); myclassa theC = new myclassc (); myclassb thed = new myclassc (); myclassc thee = new myclassd (); myclassd thef = new myclassd (); // theoc. dosomething (Thea); // theoc. dosomething (theb); // theoc. dosomething (theC); theoc. dosomething2 (Thea); theoc. dosomething2 (theb); theoc. dosomething2 (theC); theoc. dosomething2 (thed); theoc. dosomething2 (thee); theoc. dosomething2 (thef); // theoc. dosomething3 (Thea); // compilation error.

This overload judgment rule also applies to C #. The reason why the overload is determined based on the variable type rather than the actual type is actually very simple, because the overload is during the compilation period.
Will be determined. At this time, the variable type is also determined, so it is better to judge, and the actual type is often known during the runtime, of course, it is impossible to load according to the runtime
(This is also an advantage and disadvantage of a strong language ).
PS: constructor can also be overloaded. Note that if you call another constructor In the constructor, You must place it in the first row of the constructor and only call it once. This rule is in C #.
The same is true, but the writing syntax is different.

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.