虽然这段代码来自算法文本,但我在嵌套类和接口(interface)方面遇到了一些麻烦 - 实际上,我 90% 的困惑来自于这段代码如何实现接口(interface)。再次强调,这个问题与算法本身无关。
据我了解,这段代码使用了一个嵌套类,以便它可以访问ResizingArrayStack中的私有(private)实例变量(本文使用约定将所有实例变量声明为私有(private)以进行封装)。
Iterable
接口(interface)是这样的:
public Iterator<Item> { Iterator<Item> iterator(); } // ignore the quotes
而Iterator
接口(interface)是这样的:
public interface Iterator<Item> { boolean hasNext(); Item next(); void remove(); }
我的问题是所有这些如何在下面所示的代码中连接。
父类实现了Iterable接口(interface),但是当ReverseArrayIterator实现了Iterator时,Iterator接口(interface)直接来自哪里呢?它是来自 Iterator 实例方法,还是来自 Iterable 接口(interface)?直觉告诉我,它直接从 Iterator 实例方法实现,并最终从 Iterable 接口(interface)实现(有点像扩展如何工作?)。
抱歉我缺乏 OOP 知识..这篇文章只是简单地讨论了一下,我被告知我不需要知道这些(而且我可能不需要知道,只要我了解算法即可) ),但我必须理解它,哈哈。我就是无法将这件事从我的脑海中抹去。提前致谢。
// from http://algs4.cs.princeton.edu/13stacks/ResizingArrayStack.java.html
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayStack<Item> implements Iterable<Item> {
private Item[] a; // array of items
private int N; // number of elements on stack
// create an empty stack
public ResizingArrayStack() {
a = (Item[]) new Object[2];
}
public boolean isEmpty() { return N == 0; }
public int size() { return N; }
// resize the underlying array holding the elements
private void resize(int capacity) {
assert capacity >= N;
Item[] temp = (Item[]) new Object[capacity];
for (int i = 0; i < N; i++) {
temp[i] = a[i];
}
a = temp;
}
// push a new item onto the stack
public void push(Item item) {
if (N == a.length) resize(2*a.length); // double size of array if necessary
a[N++] = item; // add item
}
// delete and return the item most recently added
public Item pop() {
if (isEmpty()) { throw new RuntimeException("Stack underflow error"); }
Item item = a[N-1];
a[N-1] = null; // to avoid loitering
N--;
// shrink size of array if necessary
if (N > 0 && N == a.length/4) resize(a.length/2);
return item;
}
public Iterator<Item> iterator() { return new ReverseArrayIterator(); }
// an iterator, doesn't implement remove() since it's optional
private class ReverseArrayIterator implements Iterator<Item> {
private int i = N;
public boolean hasNext() { return i > 0; }
public void remove() { throw new UnsupportedOperationException(); }
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
return a[--i];
}
}
}
最佳答案
The parent class implements the Iterable interface, but where is the Iterator interface coming directly from when ReverseArrayIterator implements Iterator?
这个问题没有多大意义。 Iterator接口(interface)“来自”Java SE 类库(因为您的代码导入了它),并且由 ReverseArrayIterator
类实现。在运行时,对 ResizingArrayStack.iterator()
方法的调用会创建 ReverseArrayIterator
类的实例。
Intuition tells me that it's implementing directly from the Iterator instance method and ultimately from the Iterable interface (kind of like how extends work?).
与Iterable
接口(interface)的联系是外部类实现了它。但 Iterable 只是“意味着”有一个 iterator() 方法,可以调用该方法来创建适当类型的 Iterator 实例。没有发生特殊的“类似如何延长工作”的魔法。在这个例子中,这只是实现接口(interface)的类。
另一个“不同”的事情是,ReverseArrayIterator 是一个嵌套类,因此可以访问父对象的私有(private)状态。在本例中,a
和 N
。
这里发生的事情是,许多独立的事物(语言特性和设计模式)被组合起来以实现特定的整体效果。总体效果是可以使用“foreach”循环迭代堆栈的元素。
关于java - java中的嵌套类和实现接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10974289/