第三章java中的方法重载


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

让我们举个例子。你想打印工资,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

方法重载规则:

Number of Arguments Overloaded method can have different number of arguments
Date type Overload method can have different data type for argument
Return type Return type can be changed but either number of argument or data type of argument should also be changed..
Order of arguments If you change sequence of arguments then it is also valid method overloading provided you have different data types arguments.
Constructor Can be overloaded

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

  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); 
    }

}

为什么我们不能只改变返回类型?

如果我们只改变返回类型,编译器将无法确定调用哪个方法。这就是为什么你不能只改变返回类型。

什么是静态绑定?

当你编译 Java 程序时。在编译过程中,编译器将方法调用绑定到实际方法。这称为静态绑定,方法重载绑定发生在编译时。


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