Java。关键字 <this>。迭代器模式

Java. Keyword <this>. Iterator pattern

今天学习了Iterator模式,但是有一段代码没看懂。你能帮我吗?

这是一个class:

public class Repository implements Container{
    public String[] names = {"John", "Iren", "Anthony", "Lorn"};

    @Override
    public Iterator getIterator() {
        return new MyIterator();
    }
    private class MyIterator implements Iterator {
        int index;
        @Override
        public boolean hasNext() {
             if (index < names.length) {
                 return true;
             }
             return false;
        }

        @Override
        public Object next() {
           if (this.hasNext()) {
               return names[index++];
           }
           return null;
        }
    }
}

以及主要方法:

public static void main(String[] args) {
        Repository name = new Repository();
        for (Iterator iter = name.getIterator(); iter.hasNext(); ) {
            String local = (String) iter.next();
            System.out.println("Name = " + local);
        }
    }

问题是关于方法 next() :

        @Override
        public Object next() {
           if (this.hasNext()) {
               return names[index++];
           }
           return null;
        }

我不明白关键字在此上下文中的含义。这是什么参考?

this 关键字是对您所在的非静态方法对象的引用。这里 thisMyIterator 对象的 next() 方法内部,因此 this 是对 MyIterator 对象的引用。请注意,在提供的代码中,您可以省略 this. 并简单地编写 if(hasNext()) {...}.

this是引用当前对象的引用变量

在实例方法或构造函数中,this 是对当前对象的引用——调用其方法或构造函数的对象。您可以使用 this 在实例方法或构造函数中引用当前对象的任何成员。

看看Java doc and here

Java中的this关键字是一个特殊关键字,可用于表示Java中任何class的当前对象或实例。 “this”关键字也可以在Java中调用同一个class的构造函数,用于调用重载的构造函数。

"this" 有时也与 super 关键字相关联,用于表示 Java 中 super class 的实例,可用于调用 Java 中的重载构造函数。

--> this keyword represent current instance of class.



--> this keyword can be used to call overloaded constructor in java. if used than it must be first statement in constructor this() will call no argument constructor and this(parameter) will call one argument constructor with appropriate parameter. here is an example of using this() for constructor chaining:


Example


"this" keyword to call constructor in Java

class Student{
    private int id;
    private String name;

    public Student(){
       this(“Student of the year”);
    }

    public Student(int id){
        this.id = id;
        this.interest = 0.0;
    }  
}


If member variable and local variable name conflict, this can be used to refer member variable.

Example #2

  public Student(int id, double name){
        this.id = id;
        this.name = name;
  }


this is a final variable in Java and you can not assign value to this. 

this = new Student(); //this will result in compilation error--cannot assign value to final variable : this

Or you can call methods of class by using this keyword 
Example #3

   public String getName(){
        return this.toString();
    }

Or this can be used to return object. this is a valid return value. 
Example #4

public Student getStudent(){
 return this;
}

在你的情况下,我认为 "this" 可以表示你当前的对象存储库包含字符串数组名称,其中每个 elements.And word/string [=] 19=] will return true its when iteration has more values,这意味着如果当前对象数组中仍有单词,它将继续在屏幕上显示人名。否则,它会将 "if" 和 return 的块取出为 null。