给定以下结构的类:
public class Section extends IterableWidgetTemplate<Item>{
private List<WebElement> items1;
// other non iterable methods
private int indexOf(final Item item) {
int i = Iterables.indexOf(this, new Predicate<Item>() {
. . .
});
return i;
}
哪里Iterables
是 Guava com.google.common.collect.Iterables
根据其文档,它包含对 Iterable
类型的对象进行操作的静态实用方法。 .
现在在我上面描述的类中,this
作为可迭代对象传递给 private int indexOf()
方法。
问题:
- 我要在
this
中迭代什么目的?我的假设是对的吗Iterables
类将使用传递给它的对象中唯一可用的可迭代方法?所以在这种情况下我们有List<WebElement>
里面的变量this
对象。 - 如果 1. 的答案是"is",那么
Section
会发生什么?类有多个可迭代变量吗?其中哪一个将用于迭代?
最佳答案
Iterables.indexOf()
将实现 Iterable
接口(interface)的对象作为其第一个参数。因此,Iterables.indexOf()
迭代的内容是由作为参数传入的对象定义的,在您的示例中是 Section
类。但它不使用变量 - 它将调用 Section
对象上的 Iterable.iterator()
方法。不可能有多个这些方法,因此不会出现对 Iterables.indexOf()
将迭代的内容产生混淆的情况。
关于java - 使用 Guava Iterables 时将使用什么 Iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35767504/