AsyncTask的的doInBackground(参数... PARAMS)参数、AsyncTask、doInBackground、PARAMS

2023-09-12 11:30:57 作者:与君共勉

我不熟悉的 doInBackground(参数... PARAMS)

这是什么类型的东西叫,以及如何利用它?

What is this type of thing called, and how do I utilize it?

推荐答案

随着德瓦和VVV说,被称为可变参数。实际上,下面两行code是等价的:

As devA and VVV have said, that is called "varargs". Effectively, the following two lines of code are equivalent:

public void makeLemonade(String[] args) {

public void makeLemonade(String... args) {

该方法内的code将是相同的,但是当它被调用时,它们将被称为不同。第一个需要被称为是这样的:

the code inside the method would be the same, but when it was called, they would be called differently. The first would need to be called like this:

makeLemonade(new String[]{"lemon1", "lemon2", "lemon3"});

,而第二个的方法,签名可以具有0到的参数(一个假定的)无限数量,但是他们都需要是字符串参数。下面所有的调用将工作:

while the second one's method signature could have 0 to (an assumed)infinite number of arguments, but they would all need to be String arguments. All of the following calls would work:

makeLemonade("lemon1");
makeLemonade("lemon4", "lemon7", "lemon11", "lemon12"); 
makeLemonade();
// ... etc ...

两者之间的细微差别是,你可以,如果你使用的是可变参数调用makeLemonade()依法在这里。

A subtle difference between the two is that you can call makeLemonade() legally here if you're using varargs.