簡體   English   中英

Java。 關鍵詞 <this> 。 迭代器模式

[英]Java. Keyword <this>. Iterator pattern

今天,我學習了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;
        }

在這種情況下,我不了解關鍵字的含義。 這是什么參考?

this關鍵字是對您所使用的非靜態方法所在對象的引用。 this是在MyIterator對象的next()方法內部,因此this是對MyIterator對象的引用。 請注意,在提供的代碼中,您可以忽略this. 並簡單地編寫if(hasNext()) {...}

是引用當前對象的引用變量

在實例方法或構造函數中, this是對當前對象的引用-當前對象正在調用其方法或構造函數。 您可以使用此方法從實例方法或構造函數中引用當前對象的任何成員。

在這里看看Java文檔

Java中的this關鍵字是一個特殊的關鍵字,可用於表示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”可以表示您當前的對象存儲庫包含String數組名稱,每個元素中的單詞/字符串均包含John,Iren,Anthony,Lorn。並且“ this.hasnext()”將在迭代時返回true具有更多的值,這意味着如果當前對象數組中仍然有單詞,它將繼續在屏幕上顯示此人的名字。 否則,它將退出“ if”塊並返回null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM