编写一个 Java 程序,使用方法重载的概念来查找算术和。在此示例中,我们创建了四个具有相同名称但参数不同的方法来执行算术加法或求和。每次调用 add 函数时,Javac 都会根据参数调用适当的方法。
package NumPrograms;
public class SumMethodOverloading {
int add(int a, int b)
{
int sum = a + b;
return sum;
}
int add(int a, int b, int c)
{
return a + b + c;
}
int add(int a, int b, int c, int d)
{
return a + b + c + d;
}
int add(int a, int b, int c, int d, int e)
{
return a + b + c + d + e;
}
public static void main(String[] args) {
SumMethodOverloading obj = new SumMethodOverloading();
int sum = obj.add(10, 20);
System.out.println("The Sum of Two Numbers = " + sum);
System.out.println("The Sum of Three Numbers = " + obj.add(10, 20, 40));
System.out.println("The Sum of Four Numbers = " + obj.add(15, 25, 49, 66));
System.out.println("The Sum of Five Numbers = " + obj.add(11, 19, 16, 72, 66));
}
}
