public class InstrumentedHashSet<E> extends HashSet {
private long count;
@Override
public boolean add(Object o) {
countPP;
return super.add(o);
}
@Override
public boolean addAll(Collection collection) {
count += collection.size();
return super.addAll(collection);
}
public long getCount() {
return count;
}
public static void main(String[] args) {
HashSet hashSet = new HashSet(3);
hashSet.add(1);
hashSet.add(2);
hashSet.add(3);
InstrumentedHashSet<Integer> instrumentedHashSet = new InstrumentedHashSet<>();
instrumentedHashSet.addAll(hashSet);
//63
System.out.println(instrumentedHashSet.getCount());
}
}
---------------------
this first calls the addAll, of the object of instrumentedHashSet, and then count becomes 3, and then calls super.addAll, that is, the addAll in hashset"s addAll,hashset should call hashset"s add, right? Then why is count 6? shouldn"t it be 3?
I debugged the program and found that it was the addAll in hashset that called InstrumentedHashSet"s add,. Why? Is it because the add method is overridden? The parent class adjusts its own method, only to find that the quilt class is overwritten, so the method of the subclass is adjusted.
The subclass calls method A, and if it is not overridden, the parent class"s method An is called, and if overridden, its own method An is called.
does the parent class call its own method A to see if it is overridden by the subclass? A little confused.
question asks: what kind of call chain is this? (write down all your thoughts, it may be a little messy, don"t spray for beginners.)