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

时间:2015-07-04 15:54:13

标签: java design-patterns iterator this

今天我研究了Iterator模式,但我并不太了解一堆代码。你能帮帮我吗?

这是一个班级:

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;
        }

在这种情况下,我不理解关键字的含义。这是什么参考?

3 个答案:

答案 0 :(得分:1)

this关键字是对象的引用,您使用的是非静态方法。此处next()位于MyIterator对象的this方法内,因此MyIterator是对this.对象的引用。请注意,在提供的代码中,您可以省略if(hasNext()) {...}并简单地编写var myAPI = { method: function () {} }; var spy = sinon.spy(); var mock = sinon.mock(myAPI); mock.expects("method").once().throws(); PubSub.subscribe("message", myAPI.method); PubSub.subscribe("message", spy); PubSub.publishSync("message", undefined); mock.verify(); assert(spy.calledOnce);

答案 1 :(得分:0)

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

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

查看Java dochere

答案 2 :(得分:0)

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

“this”有时也与super关键字相关联,super关键字用于表示Java中的超类实例,可用于在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”可以代表你当前的Object Repository包含String数组名,其中包含John,Iren,Anthony,Lorn作为每个元素中的单词/字符串。“this.hasnext()”将返回true当迭代具有更多值时,这意味着如果当前对象数组中仍有单词,它将继续向屏幕显示该人的姓名。否则,它将取出“if”块并返回null。