* Method of variable number of formal parameters:
* 1. Format: For method parameters: Data Type ... Formal parameter name
* 2. The method of a variable number of parameters forms an overload between the method with the same name
* 3. Variable number of formal parameters at the time of invocation, the number starts from 0, to infinitely many can.
* 4. The use of variable multiple formal parameters is consistent with the use of arrays for method parameters.
* 5. If there is a variable number of parameters in the method, be sure to declare at the end of the method parameter.
* 6. In one method, declare at most one variable number of parameters.
Public classTestargs { Public Static voidMain (string[] args) {Testargs T=NewTestargs (); T.sayhello (); //T.sayhello (New string[]{"Hello China", "Hello Beijing"});T.sayhello ("Hello China", "Hello Beijing"); } //The following four methods form an overload//once the method of the overloaded variable number of parameters is defined in the class, the following two methods can be omitted//Public void SayHello () {//System.out.println ("Hello world!");// }//Public void SayHello (String str1) {//System.out.println ("Hello" + str1);// } //method of parameter with variable number Public voidSayHello (String ... args) { for(inti = 0;i < args.length;i++) {System.out.println (Args[i]+ "$"); } //System.out.println ("====="); } Public voidSayHello (inti,string ... args) { //Public void SayHello (String ... args,int i) {System.out.println (i); for(intj = 0;j < args.length;j++) {System.out.println (Args[j]+ "$"); } } Public voidsayHello1 (string[] args) { for(inti = 0;i < args.length;i++) {System.out.println (args[i]); } } //examples of the use of variable number parameters//public int Getsum (int i,int j) {//return i + J;// }//public int getsum (int i,int j,int k) {//return i + j + K;// } Public intGetsum (int.. args) { intsum = 0; for(inti = 0;i < args.length;i++) {sum+=Args[i]; } returnsum; } }
Java Learning--a method of changing the number of formal parameters