[英]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()
方法返回其实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.