'this' 关键字在 DataStructureIterator iterator = this.new EvenIterator(); 的上下文中指的是什么;

what 'this' keyword is referring to within the context of DataStructureIterator iterator = this.new EvenIterator();

我是 Java 的新手,下面的代码来自 Java Oracle 教程。

有两个问题我很疑惑

1) 有人可以告诉我 "this" 关键字在

的上下文中指的是什么吗
DataStructureIterator iterator = this.new EvenIterator();

我已从语句中删除 'this' 关键字,一切似乎都正常。 'this' 关键字是否服务于某些我不知道的特殊功能,或者它是否多余?

2)

有什么用
interface DataStructureIterator extends java.util.Iterator<Integer> { }

真的有必要吗?因为我已经从代码中删除了它(以及一些小的相关更改)并且一切正常。

public class DataStructure {

    // Create an array
    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];

    public DataStructure() {
        // fill the array with ascending integer values
        for (int i = 0; i < SIZE; i++) {
            arrayOfInts[i] = i;
        }
    }

    public void printEven() {

        // Print out values of even indices of the array
        DataStructureIterator iterator = this.new EvenIterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
    }

    interface DataStructureIterator extends java.util.Iterator<Integer> {
    }

    // Inner class implements the DataStructureIterator interface,
    // which extends the Iterator<Integer> interface
    private class EvenIterator implements DataStructureIterator {

        // Start stepping through the array from the beginning
        private int nextIndex = 0;

        public boolean hasNext() {

            // Check if the current element is the last in the array
            return (nextIndex <= SIZE - 1);
        }

        public Integer next() {

            // Record a value of an even index of the array
            Integer retValue = Integer.valueOf(arrayOfInts[nextIndex]);

            // Get the next even element
            nextIndex += 2;
            return retValue;
        }
    }

    public static void main(String s[]) {

        // Fill the array with integer values and print out only
        // values of even indices
        DataStructure ds = new DataStructure();
        ds.printEven();
    }
}

DataStructureIterator 扩展了 java.util.Iterator<Integer> 而不添加任何新方法。因此,任何使用它的地方都可以安全地替换为 java.util.Iterator<Integer>.

this.new EvenIterator()中的this指的是当前DataStructure实例,作为内EvenIteratorclass实例的封闭实例正在该声明中实例化。由于您是从封闭的 class DataStructure 的实例中创建 EvenIterator 的实例,因此无需明确指定它,并且 new EvenIterator() 有效。