Java Learning 3 Member methods and function overloading

Source: Internet
Author: User

Method Definition: Method name, return value, parameter list, modifier (permission modifier, final,static), implement body.

Reference from: Java Programming and Engineering practice

Signature of the method:

The only element that distinguishes the other method: (1) the method name; (2) the list of parameter types (parameter order and type). This is the signature of the method.

The signature of the method does not include the return value of the method. Although each overloaded method can have a different return type, the return type is not sufficient to distinguish which method is being used.

Example: (1) public void on () {}

public int on () {}; Two method compilers are still considered the same method and cannot be defined in a class at the same time.

(2) pubic void on () {}

private void On () {}; method modifiers are different, but the compiler thinks it is the same method and cannot be defined in the same class.

(3) public void on (int a,int b) {}

public void on (int b,int a) {}; only parameter names are different.

Overload:

A method with the same method name in a class but a different list of parameter types is called overloading.

Example:public class light{

public void on () {}

public void on (int color) {}

}

Overloaded technology is actually more because of the need to abstract and describe problems. If the lights are turned on, but only because the specifics are different, and to declare multiple functions of different names, the readability of the entire code becomes worse.

Example

The System.out.println () print function used to print symbols in the console. It has multiple overloads.

public void println ();

public void println (Boolean x);

public void println (char x), and so on.

Example

Constructors use overloaded functions, which can be the cause of overloading techniques.

public class light{

Public light () {}

public light (int color) {}

public static void Main (string[] args) {

Light light = new Light ();

Light light = new Light (10);}

}

The new object must be followed by the name of the class, followed by parentheses to indicate that the constructor is called, and the parameter type list of the function uniquely distinguishes the calling constructor.

Design problems with function overloading:

Overloading does not have to be used deliberately, in the design and programming process naturally think of the function defined by the name of the behavior, the behavior of the same function naturally defines the same function name.

Example

Functions are similar, and the functions accept the same parameters.

public void on (int color);

public void on (int wattage);

The function signature looks the same, but the compiler cannot differentiate between the two functions, and a compilation error occurs.

Resolution: Renaming one of the functions;

Java Learning 3 Member methods and function overloading

Related Article

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.