Java Basics---Method re-cutting in Java (41)

Source: Internet
Author: User
Tags string format

Overloading of methods in Java

Q: What are the overloads of a method?

A: If the same class contains two or more than two methods with the same name, the number of method arguments, the order, or a different type of method, it is called an overload of the method, or it can be said that the method is overloaded. the 4 method names shown below are show, but the parameters of the methods are different and therefore belong to the overloads of the method:

650) this.width=650; "style=" width:340px; src= "Http://img.mukewang.com/5375c8f000010ecb06410521.jpg"/>

Q: How do you differentiate which overloaded method is called?

A: When calling overloaded methods, Java determines which overloaded method should be called based on the number and type of parameters, and the method that exactly matches the parameters is executed. Such as:

650) this.width=650; "style=" width:340px; src= "Http://img.mukewang.com/5375caf20001394507830169.jpg"/>

Operation Result:

650) this.width=650; "src=" http://img.mukewang.com/5375de2300017c8e01430090.jpg "/>

The basis for judging method overloading:

1. Must be in the same class

2. Same method name

3, the number of method parameters, order or type is different

4, not related to the modifier or return value of a method


Code:

public class HelloWorld {
public static void Main (string[] args) {

Creating objects
HelloWorld Hello = new HelloWorld ();

Calling a method without a parameter
Hello.print ();

Calling a method with a string argument
Hello.print ("string format");

Call a method with an integer parameter
Hello.print (4);
}

public void print () {
SYSTEM.OUT.PRINTLN ("Non-parametric Print method");
}

public void print (String name) {
System.out.println ("Print method with a string argument with the parameter value:" + name);
}

public void print (int age) {
System.out.println ("Print method with an integer parameter with the parameter value:" + age);
}
}



Operation Result:

The Print method without parameters
Print method with a string argument with the parameter value: string format
Print method with an integer parameter value of: 4

This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1548011

Java Basics---Method re-cutting in Java (41)

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.