📜  重载Java的可变参数方法

📅  最后修改于: 2022-05-13 01:55:01.756000             🧑  作者: Mango

重载Java的可变参数方法

在这里,我们将讨论 varargs / variable arity 方法以及我们如何重载这种类型的方法。所以让我们首先了解什么是可变元方法及其语法。变量 arity 方法也称为 varargs 方法,可以采用多个指定类型的变量。

句法:

methodName(dataType...variableName)

“……”表示该方法可以采用任意数量的指定数据类型的参数,甚至为零。内部可变参数方法是使用一维(1D)数组概念实现的。

示例 1:



Java
// Java Program to Illustrate Variable arity Method
  
// Importing required classes
import java.io.*;
  
// Main class
class GFG {
  
    // Method 1
    // Main driver method
    public static void main(String[] args)
    {
        // Calling varargs sum method with no parameter
        sum();
  
        // Calling varargs sum method with two int type
        // parameters
        sum(10, 10);
  
        // Calling varargs sum method with three int type
        // parameters
        sum(15, 15, 20);
    }
  
    // Method 2
    // varargs method expects zero or
    // more int type parameters
    public static void sum(int... x)
    {
        int total = 0;
  
        // Takes paramaters passed to sum, one at a time
        // later summing it up
        for (int y : x) {
            total += y;
        }
  
        // Print the sum on console
        System.out.println("Sum : " + total);
    }
}


Java
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        sum(9);
        // Since we are passing two int type arguments and
        // we have defined a function which expects two int
        // type parameters so that particular method (normal
        // sum method ) will be called and not varargs
        // method.
        sum(10, 20);
    }
  
    // normal maethod which expects two int type parameters
    public static void sum(int x, int y)
    {
        System.out.println("normal method");
        System.out.println("Sum : " + (x + y));
    }
  
    // varargs method which expects zero or more int ype
    // parameters
    public static void sum(int... x)
    {
        System.out.println("varargs method");
        int total = 0;
        for (int y : x) {
            total += y;
        }
        System.out.println("Sum : " + total);
    }
}


Java
// Java Program to illustrate Overloading in Variable
// arity
  
// Importing input output classes
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        vaTest(1, 2, 3, 4, 5);
        vaTest(true, true, false, true);
        vaTest("Message", 10, 20);
    }
  
    // Method 1
    // varargs method which expects zero or
    // more int type parameters
    public static void vaTest(int... x)
    {
  
        // Print statement on console
        System.out.println(
            "varargs method with int type arguments");
  
        for (int y : x) {
            System.out.print(" " + y);
        }
        System.out.println();
    }
  
    // Method 3
    // varargs method which expects zero or
    // more boolean type parameters
    public static void vaTest(boolean... x)
    {
  
        // Print statement on console
        System.out.println(
            "varargs method with boolean type arguments");
  
        for (boolean y : x) {
            System.out.print(" " + y);
        }
        System.out.println();
    }
  
    // Method 3
    // varargs() method which expects first parameter to be
    // of String type and then zero or more int type
    // parameters.
    public static void vaTest(String msg, int... x)
    {
  
        // Print statement on console
        System.out.print(msg);
  
        for (int y : x) {
            System.out.print(" " + y);
        }
  
        // New line
        System.out.println();
    }
}


输出
Sum : 0
Sum : 20
Sum : 50

现在让我们看一下有关 varargs 方法和可接受的参数顺序的一些重要事实。以下几点将有助于提高对 varargs 方法的理解,如下所示:

1.1 Varargs 参数可以与普通参数混合使用。

methodName(int x, String...y)
methodName(10)  Valid
methodName(10 , “Hello”)  Valid

1.2混合时,varargs 参数应放在最后

methodName(int x, String…y) valid
methodName(int…x, String y)invalid

1.3 vararg 参数只能有一个

methodName(int x, int y, String…z)valid
methodName(int…x, String…y)invalid

1.4一般情况下,可变参数,如果没有其他方法匹配则只有可变参数的方法将被调用的方法会得到最后的优先手段。 下面的例子将跨越这个概念。

例子:

Java

import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        sum(9);
        // Since we are passing two int type arguments and
        // we have defined a function which expects two int
        // type parameters so that particular method (normal
        // sum method ) will be called and not varargs
        // method.
        sum(10, 20);
    }
  
    // normal maethod which expects two int type parameters
    public static void sum(int x, int y)
    {
        System.out.println("normal method");
        System.out.println("Sum : " + (x + y));
    }
  
    // varargs method which expects zero or more int ype
    // parameters
    public static void sum(int... x)
    {
        System.out.println("varargs method");
        int total = 0;
        for (int y : x) {
            total += y;
        }
        System.out.println("Sum : " + total);
    }
}
输出

varargs method
Sum : 9
normal method
Sum : 30

1.5使用数组传递可变数量的参数是varargs的旧方法,不适合。

所以让我们来讨论重载 varargs 方法。我们知道,如果一个类有多个名称相同但参数不同的方法,则称为方法重载。这同样适用于 varargs 方法。让我们实现相同的

实现:这里 'vaTest' 被重载,因为每次出现 vaTest 都需要不同的参数列表。哪个 vaTest 会被调用来执行对参数的操作取决于每个参数的类型。

例子:

Java

// Java Program to illustrate Overloading in Variable
// arity
  
// Importing input output classes
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        vaTest(1, 2, 3, 4, 5);
        vaTest(true, true, false, true);
        vaTest("Message", 10, 20);
    }
  
    // Method 1
    // varargs method which expects zero or
    // more int type parameters
    public static void vaTest(int... x)
    {
  
        // Print statement on console
        System.out.println(
            "varargs method with int type arguments");
  
        for (int y : x) {
            System.out.print(" " + y);
        }
        System.out.println();
    }
  
    // Method 3
    // varargs method which expects zero or
    // more boolean type parameters
    public static void vaTest(boolean... x)
    {
  
        // Print statement on console
        System.out.println(
            "varargs method with boolean type arguments");
  
        for (boolean y : x) {
            System.out.print(" " + y);
        }
        System.out.println();
    }
  
    // Method 3
    // varargs() method which expects first parameter to be
    // of String type and then zero or more int type
    // parameters.
    public static void vaTest(String msg, int... x)
    {
  
        // Print statement on console
        System.out.print(msg);
  
        for (int y : x) {
            System.out.print(" " + y);
        }
  
        // New line
        System.out.println();
    }
}
输出
varargs method with int type arguments
 1 2 3 4 5
varargs method with boolean type arguments
 true true false true
Message 10 20