java中的方法重载


java中的方法重载

如果两个或多个方法具有相同的名称,但参数不同,则调用它method overloading

你为什么要这样做(同名但不同的论点)?

让我们举个例子。你想打印工资,employee有时公司给他们的员工奖金,有时不。所以如果公司不给奖金,那么我们可以使用printSalary(int salary)方法,如果它提供奖金,那么我们可以使用,printSalary(int salary,int bonus)所以两种方法都在做同样的工作但是它们的输入是不同的,所以它会增加程序的可读性。否则如果你给不同的方法名称,就会变得难以理解。

package org.arpit.java2blog;

public class Employee{

    public void printSalary(int salary)
    {
        System.out.println("Salary without bonus : "+salary);

    }

    public void printSalary(int salary,double bonus)
    {
        System.out.println("Salary with bonus : "+(salary+bonus));

    }
    public static void main(String args[])
    {

        Employee e1=new Employee();
        // if no bonus provided, we can use this method
        e1.printSalary(20000);
        System.out.println("**********************");
        // If bonus provided we can pass to overloaded method and add to salary
        e1.printSalary(20000, 10000);
    }
}

当你运行上面的程序时,你会得到以下输出:

Salary without bonus : 20000
**********************
Salary with bonus : 30000

方法重载规则:

参数数量 重载方法可以有不同数量的参数
日期类型 重载方法的参数可以有不同的数据类型
返回类型 返回类型可以改变,但参数的数量或参数的数据类型也应该改变。
参数顺序 如果您更改参数序列,那么只要您有不同的数据类型参数,它也是有效的方法重载。
构造函数 可以超载

因此,您可以使用三种方式重载方法:

  1. 通过改变参数的数量
  2. 通过改变参数的数据类型
  3. 通过改变参数的序列,如果它们是不同的类型

通过改变参数的数量

上面我们采用的例子就是这种类型。我们正在用不同数量的参数重载 printSalary() 方法。

通过更改参数的数据类型:

在上面的例子中,我们将创建另一个方法,它将双数据类型作为输入。

package org.arpit.java2blog;
public class Employee{

    public void printSalary(int salary)
    {
        System.out.println("Salary without bonus : "+salary);

    }

    public void printSalary(int salary,double bonus)
    {
        System.out.println("Salary with bonus : "+(salary+bonus));

    }

  public void printSalary(double salary)    {
        System.out.println("Salary without bonus : "+salary);

    }

    public static void main(String args[])

    {

         Employee e1=new Employee();
        // if no bonus provided, we can use this method
        //will call printSalary(int)
        e1.printSalary(20000);
        Employee e2=new Employee();
        // will call printSalary(double)
        e2.printSalary(30000.5);
        System.out.println("**********************");
        // If bonus provided we can pass to overloaded method and add to salary
        e1.printSalary(20000, 10000);
    }
}

所以这里我们引入了一种新方法,它以双数据类型作为输入。这也是有效的方法重载。

如果它们是不同的数据类型,则通过更改参数序列

我们可以引入一种新方法printSalary(double bonus,int salary)。所以通过改变参数的顺序我们可以重载方法。

package org.arpit.java2blog;

public class Employee{

    public void printSalary(int salary)
    {
        System.out.println("Salary without bonus : "+salary);

    }

    public void printSalary(int salary,double bonus)
    {
        System.out.println("Salary with bonus : "+(salary+bonus));

    } 
 public void printSalary(double bonus,int salary)
    {
        System.out.println("Salary with bonus : "+(salary+bonus));

    }

    public static void main(String args[])
    {

        Employee e1=new Employee();
        // if no bonus provided, we can use this method
        e1.printSalary(20000);
        System.out.println("**********************");
        // If bonus provided we can pass to overloaded method and add to salary
        e1.printSalary(20000, 10000);<
      // Changing sequence 
        e1.printSalary(2000.5, 20000); 
    }

}


原文链接:https://codingdict.com/