Meaning of “this” in this code?

前端 未结 6 864
眼角桃花
眼角桃花 2020-12-16 01:31
 public boolean contains(Object o) {
    for (E x : this)
        if (x.equals(o))
            return true;
    return false;
}

Can someone tell me

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 02:23

    Keyword this is just a reference to the current object.

    Here is a example how can be this used:

    public class Person {
        public final String name;
    
        public Person(String name) {
            // name = name; 
            // which one is an argument, and which one is class field? 
            // by default, both are reference on argument
    
            // using "this" to access class field
            this.name = name;
        }
    
        public void copyFields(Person other) {
            // current object's reference is the same as other object reference 
            // in other words "this" and "other" are the same instances 
            // example: 
            // Person p1 = new Person("a");
            // Person p2 = p1; // p2 is now pointing on the same memory address
            //                 // as p1, so both are pointing on the same object
            //                 // stored in memory.
            // p1.copyFields(p2);
            if (this == other) { // copying from self? useless...
                return;
            }
            this.name = other.name;
        }
    }
    

    Anything that implements Iterable interface has method which returns Iterator instance, which is implicitly used by foreach loop to iterate over items hold by object. Iterator has methods hasNext() which returns true, if there is another object in iterable container, relative to current position, and next() which returns next object or throws NoSuchElementException if there is no next object (last invokation of hasNext() has returned false).

    Here is a simple example of Iterable implementation with contains methods:

    public class Customer extends Person implements Iterable {
        private final List list = new LinkedList<>();
        public final String name;
    
        public Customer(String name) {
            this.name = name;
        }
    
        public void add(Item item) {
            list.add(item);
        }
    
        // implementing iterable interface
    
        @Override
        public Iterator iterator() {
            return list.iterator();
        }
    
        // some contains implementations
    
        public boolean contains1() {
            for (Item item : this) { // customer implements Iterable = OK
                if (o.equals(item)) {
                    return true;
                }
            }
            return false;
        }
    
        public boolean contains2() {
            for (Item item : list) { // list implements Iterable = OK
                if (o.equals(item)) {
                    return true;
                }
            }
            return false;
        }
    
        public boolean contains3(Object o) {
            for (Iterator iter = iterator(); iter.hasNext();   ) {
                Item item = iter.next();
                if (o.equals(item)) {
                    return true;
                }
            }
            return false;
        }
    
        public boolean contains4(Object o) {
            for (Iterator iter = list.iterator(); iter.hasNext();   ) {
                Item item = iter.next();
                if (o.equals(item)) {
                    return true;
                }
            }
            return false;
        }
    
        public boolean contains5(Object o) {
            Iterator iter = iterator(); 
            while (iter.hasNext()) {
                Item item = iter.next();
                if (o.equals(item)) {
                    return true;
                }
            }
            return false;
        }
    
        public boolean contains6(Object o) {
            Iterator iter = list.iterator();
            while (iter.hasNext()) {
                Item item = iter.next();
                if (o.equals(item)) {
                    return true;
                }
            }
            return false;
        }
    
        public boolean contains7(Object o) {
            return list.contains(o);
        }
    }
    

提交回复
热议问题