有时候,我们传入到方法的参数的个数是不固定的,为了解决这个问题,我们一般采用下面的方法:<wbr style="line-height:25px"></wbr><wbr style="line-height:25px"><br style="line-height:25px"><span style="font-size:18px; line-height:1.3em"><span style="line-height:1.3em">1.</span><wbr style="line-height:32px"><span style="font-family:宋体; line-height:1.3em">重载,多重载几个方法,尽可能的满足参数的个数。显然这不是什么好办法。</span><wbr style="line-height:32px"></wbr></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="font-size:18px; line-height:1.3em"><span style="line-height:1.3em">2.</span><wbr style="line-height:32px"><span style="font-family:宋体; line-height:1.3em">将参数作为一个数组传入。虽然这样我们只需一个方法即可,但是,为了传递这个数组,我们需要先声明一个数组,然后将参数一个一个加到数组中。</span><wbr style="line-height:32px"></wbr></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="font-family:宋体; line-height:1.3em"><span style="font-size:18px; line-height:1.3em">现在,我们可以使用可变长参数解决这个问题。</span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="font-size:18px; line-height:1.3em"><span style="font-family:宋体; line-height:1.3em">声明可变长参数方式如下</span><wbr style="line-height:32px"><span style="line-height:1.3em">:</span><wbr style="line-height:32px"></wbr></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em">publicvoidmymethod(Stringarg1,Object…args)</span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="font-size:18px; line-height:1.3em"><span style="font-family:宋体; line-height:1.3em">也就是使用</span><wbr style="line-height:32px"><span style="line-height:1.3em">…</span><wbr style="line-height:32px"><span style="font-family:宋体; line-height:1.3em">将参数声明成可变长参数。</span><wbr style="line-height:32px"></wbr></wbr></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="font-family:宋体; line-height:1.3em"><span style="font-size:18px; line-height:1.3em">显然,可变长参数必须是最后一个参数。</span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="font-family:宋体; line-height:1.3em"><span style="font-size:18px; line-height:1.3em">请看下面唐僧师傅给悟空讲佛经的例子:</span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em">packagecom.kuaff.jdk5;</span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em">publicclassVarargs1</span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em">{</span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px">publicvoidspeak(Stringname,Object...arguments)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px">{</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px">for(Objectobject:arguments)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px">{</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px">System.out.println(object);</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px">}</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px">}</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px">publicstaticvoidmain(String[]args)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px">{</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px">Varargs1va=newVarargs1();</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="font-size:18px; line-height:1.3em"><span style="line-height:1.3em"></span><wbr style="line-height:32px">va.speak("<span style="font-family:宋体; line-height:1.3em">悟空</span><wbr style="line-height:32px"><span style="line-height:1.3em">","</span><wbr style="line-height:32px"><span style="font-family:宋体; line-height:1.3em">人和妖精都是妈生的</span><wbr style="line-height:32px"><span style="line-height:1.3em">,");</span><wbr style="line-height:32px"></wbr></wbr></wbr></wbr></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="font-size:18px; line-height:1.3em"><span style="line-height:1.3em"></span><wbr style="line-height:32px">va.speak("<span style="font-family:宋体; line-height:1.3em">悟空</span><wbr style="line-height:32px"><span style="line-height:1.3em">","</span><wbr style="line-height:32px"><span style="font-family:宋体; line-height:1.3em">不同的人是人他妈生的</span><wbr style="line-height:32px"><span style="line-height:1.3em">,","</span><wbr style="line-height:32px"><span style="font-family:宋体; line-height:1.3em">妖是妖他妈生的</span><wbr style="line-height:32px"><span style="line-height:1.3em">,");</span><wbr style="line-height:32px"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px"><span style="line-height:1.3em"></span><wbr style="line-height:25px">}</wbr></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em">}</span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:1.3em"><span style="font-size:18px; line-height:1.3em"></span><wbr style="line-height:25px"></wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="font-size:18px; line-height:1.3em"><span style="font-family:宋体; line-height:1.3em">其中</span><wbr style="line-height:32px"><span style="line-height:1.3em">speak</span><wbr style="line-height:32px"><span style="font-family:宋体; line-height:1.3em">中的参数被声明成可变长的参数,所以你可以传递给</span><wbr style="line-height:32px"><span style="line-height:1.3em">speak</span><wbr style="line-height:32px"><span style="font-family:宋体; line-height:1.3em">方法多个参数。</span></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
JSE5.0新特性之可变长参数
最新推荐文章于 2022-12-31 10:12:26 发布