Chapter 4: classes and interfaces. Item16: Compound takes precedence over inheritance.

Source: Internet
Author: User
Tags addall
1 package COM. twoslow. cha4; 2 3 Import Java. util. collection; 4 Import Java. util. hashset; 5 6/** 7 * Here we need to extend the hashset class and provide new functions to count the number of elements in the current set, 8 * the implementation method is to add a private domain variable to save the number of elements, 9 * and update the value in each method that adds the new element, 10 * then provide a public method to return the value 11 */12 public class instrumentedhashset <E> extends hashset <E> {13 14 private int addcount = 0; 15 16 public instrumentedhashset () {} 17 18 public instruments entedhashset (INT initcap, float loadfactor ) {19 super (initcap, loadfactor); 20} 21 22 @ override23 public Boolean add (E) {24 addcount ++; 25 return Super. add (E); 26} 27 28/* 29 * The addall method in hashset calls the add method. The addcount method is calculated twice. 30 */31 @ override32 public Boolean addall (collection <? Extends E> C) {33 addcount + = C. size (); 34 return Super. addall (c); 35} 36 37 public int getaddcount () {38 return addcount; 39} 40 41 42}

Revised Design:

 1 package com.twoslow.cha4; 2  3 import java.util.Collection; 4 import java.util.Iterator; 5 import java.util.Set; 6  7 public class ForwardingSet<E> implements Set<E>{ 8  9     private final Set<E> s ; 10     11     public ForwardingSet(Set<E> s) {12         this.s = s ;13     }14     15     @Override16     public int size() {17         return s.size();18     }19 20     @Override21     public boolean isEmpty() {22         return s.isEmpty();23     }24 25     @Override26     public boolean contains(Object o) {27         return s.contains(o);28     }29 30     @Override31     public Iterator<E> iterator() {32         return s.iterator();33     }34 35     @Override36     public Object[] toArray() {37         return s.toArray();38     }39 40     @Override41     public <T> T[] toArray(T[] a) {42         return s.toArray(a);43     }44 45     @Override46     public boolean add(E e) {47         return s.add(e);48     }49 50     @Override51     public boolean remove(Object o) {52         return s.remove(o);53     }54 55     @Override56     public boolean containsAll(Collection<?> c) {57         return s.containsAll(c);58     }59 60     @Override61     public boolean addAll(Collection<? extends E> c) {62         return s.addAll(c);63     }64 65     @Override66     public boolean retainAll(Collection<?> c) {67         return s.retainAll(c);68     }69 70     @Override71     public boolean removeAll(Collection<?> c) {72         return s.removeAll(c);73     }74 75     @Override76     public void clear() {77         s.clear();78     }79     80     @Override81     public boolean equals(Object obj) {82         return s.equals(obj);83     }84     85     @Override86     public int hashCode() {87         return s.hashCode();88     }89     90     @Override91     public String toString() {92         return s.toString();93     }94 95 }
 1 package com.twoslow.cha4; 2  3 import java.util.Collection; 4 import java.util.Set; 5  6 public class InstrumentedSet<E> extends ForwardingSet<E>{ 7  8     private int addCount = 0 ; 9     10     public InstrumentedSet(Set<E> s) {11         super(s);12     }13     14     @Override15     public boolean add(E e) {16         addCount++ ;17         return super.add(e);18     }19     20     @Override21     public boolean addAll(Collection<? extends E> c) {22         addCount += c.size() ;23         return super.addAll(c);24     }25 26     public int getAddCount() {27         return addCount;28     }29 30     31 }

 

Chapter 4: classes and interfaces. Item16: Compound takes precedence over inheritance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.