Java 简明教程
Java - Method Overloading
Advantage of Method Overloading
方法重载提高了代码的可读性,并减少了代码冗余。方法重载还有助于实现编译时 polymorphism 。
Method overloading improves the code readability and reduces code redundancy. Method overloading also helps to achieve compile-time polymorphism.
Example of Method Overloading
如果你观察以下的示例,我们在其中创建了一个名为 Tester 的类,此类有两个名称相同(add)和返回类型的方法,唯一的区别是它们接受的参数(一个方法接受两个整数变量,另一个接受三个整数变量)。
If you observe the following example, Here we have created a class named Tester this class has two methods with same name (add) and return type, the only difference is the parameters they accept (one method accepts two integer variables and other accepts three integer variables).
class Calculator{
public static int add(int a, int b){
return a + b;
}
public static int add(int a, int b, int c){
return a + b + c;
}
}
当你基于所传递的参数调用 add() 方法时,会执行各自的方法体。
When you invoke the add() method based on the parameters you pass respective method body gets executed.
int result = Calculator.add(1,2); // returns 3;
result = Calculator.add(1,2,3); // returns 6;
Different Ways of Java Method Overloading
可以通过以下方法实现方法重载,同时在类中具有同名方法。
Method overloading can be achieved using following ways while having same name methods in a class.
-
Use different number of arguments
-
Use different type of arguments
Invalid Ways of Java Method Overloading
通过以下方法无法实现方法重载,同时在类中具有同名方法。 编译器会抱怨重复方法的存在。
Method overloading cannot be achieved using following ways while having same name methods in a class. Compiler will complain of duplicate method presence.
-
Using different return type
-
Using static and non-static methods
Method Overloading: Different Number of Arguments
你可以基于不同的参数数量实现方法重载。
You can implement method overloading based on the different number of arguments.
Example: Different Number of Arguments (Static Methods)
在该示例中,我们创建了一个 Calculator 类,它有两个具有相同名称但不同参数的静态方法,分别用于加法两个和三个 int 值。 在 main() 方法中,我们调用这些方法并打印结果。基于传递的参数类型,编译器决定要调用的方法,并相应地打印结果。
In this example, we’ve created a Calculator class having two static methods with same name but different arguments to add two and three int values respectively. In main() method, we’re calling these methods and printing the result. Based on the type of arguments passed, compiler decides the method to be called and result is printed accordingly.
package com.tutorialspoint;
class Calculator{
public static int add(int a, int b){
return a + b;
}
public static int add(int a, int b, int c){
return a + b + c;
}
}
public class Tester {
public static void main(String args[]){
System.out.println(Calculator.add(20, 40));
System.out.println(Calculator.add(40, 50, 60));
}
}
Example: Different Number of Arguments (Non Static Methods)
在该示例中,我们创建了一个 Calculator 类,它有两个具有相同名称但不同参数的非静态方法,分别用于加法两个和三个 int 值。 在 main() 方法中,我们使用 Calculator 类的对象调用这些方法并打印结果。基于传递的参数数量,编译器决定要调用的方法,并相应地打印结果。
In this example, we’ve created a Calculator class having two non-static methods with same name but different arguments to add two and three int values respectively. In main() method, we’re calling these methods using object of Calculator class and printing the result. Based on the number of arguments passed, compiler decides the method to be called and result is printed accordingly.
package com.tutorialspoint;
class Calculator{
public int add(int a, int b){
return a + b;
}
public int add(int a, int b, int c){
return a + b + c;
}
}
public class Tester {
public static void main(String args[]){
Calculator calculator = new Calculator();
System.out.println(calculator.add(20, 40));
System.out.println(calculator.add(40, 50, 60));
}
}
Method Overloading: Different Type of Arguments
你可以基于不同类型参数实现方法重载。
You can implement method overloading based on the different type of arguments.
Example: Different Type of Arguments
在该示例中,我们创建了一个 Calculator 类,它有两个具有相同名称但不同类型参数的非静态方法,分别用于加法两个 int 值和两个 double 值。 在 main() 方法中,我们使用 Calculator 类的对象调用这些方法并打印结果。基于传递的参数类型,编译器决定要调用的方法,并相应地打印结果。
In this example, we’ve created a Calculator class having two non-static methods with same name but different types of arguments to add two int values and two double values respectively. In main() method, we’re calling these methods using object of Calculator class and printing the result. Based on the type of arguments passed, compiler decides the method to be called and result is printed accordingly.
package com.tutorialspoint;
class Calculator{
public int add(int a, int b){
return a + b;
}
public double add(double a, double b){
return a + b;
}
}
public class Tester {
public static void main(String args[]){
Calculator calculator = new Calculator();
System.out.println(calculator.add(20, 40));
System.out.println(calculator.add(20.0, 40.0));
}
}