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

Java example - using Varargs in overloading methods

黄舟
Release: 2017-02-16 10:40:10
Original
1558 people have browsed it

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);
   }}
Copy after login

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
Copy after login

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



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template