Home  >  Article  >  Java  >  Java example - using Varargs in overloading methods

Java example - using Varargs in overloading methods

黄舟
黄舟Original
2017-02-16 10:40:101533browse

The following example demonstrates how to use variable parameters in overloaded methods:

/*
 author by w3cschool.cc
 Main.java
 */public class Main {
   static void vaTest(int ... no) {
      System.out.print("vaTest(int ...): " 
      + "参数个数: " + no.length +" 内容: ");
      for(int n : no)
      System.out.print(n + " ");
      System.out.println();
   }
   static void vaTest(boolean ... bl) {
      System.out.print("vaTest(boolean ...) " +
      "参数个数: " + bl.length + " 内容: ");
      for(boolean b : bl)
      System.out.print(b + " ");
      System.out.println();
   }
   static void vaTest(String msg, int ... no) {
      System.out.print("vaTest(String, int ...): " +
      msg +"参数个数: "+ no.length +" 内容: ");
      for(int  n : no)
      System.out.print(n + " ");
      System.out.println();
   }
   public static void main(String args[]){
      vaTest(1, 2, 3);
      vaTest("测试: ", 10, 20);
      vaTest(true, false, false);
   }}

The output result of running the above code is:

vaTest(int ...): 参数个数: 3 内容: 1 2 3 
vaTest(String, int ...): 测试: 参数个数: 2 内容: 10 20 
vaTest(boolean ...) 参数个数: 3 内容: true false false

The above is the Java example - overloading ( overloading) method, please pay attention to the PHP Chinese website (www.php.cn) for more related content!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn