JSE5.0新特性之可变长参数

有时候,我们传入到方法的参数的个数是不固定的,为了解决这个问题,我们一般采用下面的方法:<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值