Differential parameters of new features of J2SE5.0
晁 晁 攀 Smallnest@163.com
Sometimes, the number of parameters we have entered into the method is not fixed. In order to solve this problem, we generally use the following methods:
1. Overload, multiple times of multiple methods, and satisfy the number of parameters as much as possible. Obviously this is not a good way.
2. Parameters are incorporated as an array. Although we only need a method, but in order to pass this array, we need to declare an array first, then add a parameter to an array.
Now we can use variable long parameters to solve this problem.
Declare the variable length parameters as follows:
Public void mymethod (String Arg1, Object ... ARGS)
That is to use ... declare the parameters into variable length parameters.
Obviously, the variable length parameters must be the last parameter.
Please see the example of the Tang Yumi to give the Buddhist scriptures:
package com.kuaff.jdk5; public class Varargs1 {public void speak (String name, Object ... arguments) {for (Object object: arguments) {System.out.println (object);}} public static void main (String [] args) {varargs1 va = new varargs1 (); va.speak ("Wukong", "people and" people "); va.speak (" Wukong "," Different people are fucking fucking Born, "," the demon is a demon, ");}}
Where parameters in the Speak are declared into a growing parameter, you can pass multiple parameters to the Speak method.