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)