inheritance and Iterable
public interface IPowerList<T> extends Iterable< T > {
public int bitFlag(List<T> subseq);
}
public class PowerList<T> implements IPowerList<T>{
private List<T> originalList;
private Set<Set<T>> powerList= new HashSet<Set<T>>();
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public int bitFlag(List<T> subseq) {
// TODO Auto-generated method stub
return 0;
}
}
Automatically I was given by Eclipse to implement an override method for iterator
.
(1) Is it because I implement an interface that extends from iterable?
(2) My private variables are sets and lists- they have their own iterators, I don't need to define any new iterator- so what should I do? should I erase the inheritance?
(3) when do we need to define a new iterator? when we define a class that it's kind of collection for us? but I wonder why, cause we always use a generic collections, that have their own iterators..Can you give me an example?
(4) if I add to a set of sets a new HashSet<T>()
will you consider it for adding an empty list or add开发者_StackOverflow社区ing new HashSet().add(null)
will be consider as an empty list?
(1) Is it because I implement an interface that extends from iterable?
Yes, you get the iterator()
method because your interface extends Iterable
. That means that any class that implements your interface must also implement all the methods of Iterable
. If you didn't want that, don't extend Iterable
.
(2) My private variables are sets and lists- they have their own iterators, I don't need to define any new iterator- so what should I do? should I erase the inheritance?
Your private members aren't available to consumers of your class, so you need a way to expose those iterators. How you do that is up to you, but doing so through your iterate()
method makes a lot of sense.
(3) when do we need to define a new iterator? when we define a class that it's kind of collection for us? but I wonder why, cause we always use a generic collections, that have their own iterators..Can you give me an example?
If you create your own collection that you want to be iterable, but that doesn't extend a collection that already implements Iterable
, then you'll need to create your own iterator. If you don't care, then there's no need, though your consumers might feel differently.
(1) Yes, since PowerList<T> implements IPowerList<T>
, it must implement Iterator<T> iterator()
(or return a subtype of Iterator<T>
).
(2) Leave the inheritance to Iterable<T>
, that way you can encapsulate your fields (they are private anyways) and only export an Iterator. Secondly, you can decide which and what kind of Iterator you export, and are more flexible in the future (e.g. change the order of enumeration). Finally, you could think of not extending Iterable for your IPowerList if that has nothing to do with Iterations (it seems to me that way), but rather make PowerList implement both interfaces (multiple inheritance on interfaces is allowed in Java).
(3) For instance when you compose a new Collection, e.g. your set of sets. Or when you want to change the way an iterator behaves, e.g. the order of enumeration or whether the iterator may add and remove elements. For example, I have a tree structure that can return a bunch of different iterators (and implements Iterator and several similar interfaces).
精彩评论