什么是可选参数列表 [java]

本文介绍了Java中的可选参数列表(不定参数),通过举例和解析AsyncTask类中的相关方法,展示了如何使用这种参数形式。可选参数允许传递任意数量的参数,通常放在参数列表的最后,适用于参数个数不确定的情况。
摘要由CSDN通过智能技术生成


最近在学习Smack类库的API,和以往一样,我都是先从接口入手,今天在看到实现接口PacketFilter的一个类AndFilter。它有一个很奇怪的方法概要:


AndFilter

public AndFilter(PacketFilter... filters)
Creates an AND filter using the specified filters.
Parameters:
filters - the filters to add.

注意到参数列表里有3个点,很是奇怪,于是百度了一下(坏习惯啊,以后应该尽量用谷歌的),原来这三个点在JAVA中定义为可选参数(有的也称为不定参数),正好以前学过C#,这不就是C#里郑宇军老师讲过的数组型参数吗?(public double test(params double[] array) ),只不过在JAVA里改成了三个点。再细想一下,以前在android类库里好像也有一个类的方法有过这种参数表示方法,翻翻字典,原来是AsyncTask类里的一个方法。

protected abstract Result doInBackground (Params... params)
Added in  API level 3

Override this method to perform a computation on a background thread. The specified parameters are the parameters passed to execute(Params...) by the caller of this task. This method can call publishProgress(Progress...) to publish updates on the UI thread.

Parameters
params The parameters of the task.
Returns
  • A result, defined by the subclass of this task.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值