简体   繁体   English

此关键字不代表对象

[英]this keyword doesn't represent an object

Does anyone know why can we loop through the "this" keyword here (in the subsetOf method)? 有谁知道为什么我们可以在这里循环“this”关键字(在subsetOf方法中)? To my knowledge this represents a JAVA object. 据我所知,这代表了一个JAVA对象。 Some extensive explanations are welcomed, would like to know why "this" can work in this way. 欢迎一些广泛的解释,想知道为什么“这个”可以这样工作。

public class ArrayListSet<E> implements Set<E> {

    private ArrayList<E> elements;

    public ArrayListSet() {
        elements = new ArrayList<>();
    }

    @Override
    public void add(E e) {
        if (!elements.contains(e))
            elements.add(e);
    }

    @Override
    public void remove(E e) {
        elements.remove(e);
    }

    @Override
    public boolean contains(Object e) {
        return elements.contains(e);
    }

    @Override
    public int size() {
        return elements.size();
    }

    @Override
    public boolean subsetOf(Set<?> other) {
        for (E e : this) {
            if (!other.contains(e))
                return false;
        }
        return true;
    }
}

You are declaring a class which implements the Set interface which itself extends the Iterable through the Collection interface. 您正在声明一个实现Set 接口的类,该接口本身通过Collection接口扩展Iterable Any Object which is an implementation of the Iterable interface can be used inside a for-each loop. 任何作为Iterable接口实现的Object都可以在for-each循环中使用。

So the inheritance hierarchy is this for your class: 所以继承层次结构对于您的类来说是这样的:

 Iterable
    ^
    |
Collection
    ^
    |
   Set
    ^
    |
ArrayListSet

Also the this keyword always refers to the current instance. this关键字也始终引用当前实例。 In your code when you execute the subsetOf() the this will point to an instance of ArrayListSet (which was used to invoke the subSetOf() method) which is a type of Iterable through inheritance, so you can use it in a for-each loop. 在执行subsetOf()的代码中, this将指向ArrayListSet的实例(用于调用subSetOf()方法),这是一种通过继承的Iterable ,因此您可以在for-each中使用它环。

Also since you are implementing a Set you need to supply an implementation of the Iterator<E> iterator(); 此外,由于您正在实现Set您需要提供Iterator<E> iterator(); method defined in the Set interface to make it an Iterable . Set接口中定义的方法使其成为Iterable

As implementations of the Iterable interface requires a iterator() method which returns an instance of the Iterator object. 由于Iterable接口的实现需要iterator()方法,该方法返回Iterator对象的实例。 The Iterator Object will be used by the for-each loop to iterate over the elements of the ArrayListSet . for-each循环将使用Iterator对象迭代ArrayListSet的元素。

The implementation of the Iterator type returned from the iterator() method actually defines the iteration logic used by the for-each loop. iterator()方法返回的Iterator类型的实现实际上定义了for-each循环使用的迭代逻辑。

To use any Object in a for-each loop: 要在for-each循环中使用任何Object:

Step 1: Extend Iterable directly or through other Iterable types. 步骤1:直接或通过其他Iterable类型扩展Iterable

Step 2: Provide implementation of the Iterator iterator() method inherited from the Iterable interface. 第2步:提供从Iterable接口继承的Iterator iterator()方法的实现。

Step 3: Implement an Iterator and return its instance from the iterator() method. 第3步:实现Iterator并从iterator()方法返回其实例。

You can iterate through this since it represents a set. 你可以迭代这个,因为它代表一个集合。 The set is iterable . 可迭代的 Therefore it is valid syntax. 因此它是有效的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM