2016-03-03 12 views
0

提供类以下结构:什么可迭代将与番石榴Iterables工作时,可以使用

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是番石榴com.google.common.collect.Iterables,根据其文档,包含了对对象进行操作的静态实用方法键入Iterable

现在在我上面描述的类中,this作为iterable传递给private int indexOf()方法。

问题:

  1. 我该怎么在this对象遍历?我是否正确假设Iterables类将使用传递给它的对象中唯一可用的迭代方法?所以在这种情况下,我们有内部this对象List<WebElement>变量。
  2. 如果1.的答案是“是”,如果Section类有多个可迭代变量会发生什么?哪一个将用于迭代?

回答

2

Iterables.indexOf()将第一个参数作为实现Iterable接口的对象。所以,Iterables.indexOf()迭代的内容由作为参数传入的对象定义,在您的示例中为Section类。但它没有使用变量 - 它会调用Section对象上的Iterable.iterator()方法。不可能有多于一种这样的方法,因此不会有任何关于Iterables.indexOf()将迭代的混淆的情况。