问题
I am relatively new to Java Programming and I came across this declaration of an interface and a class that implements it:
public interface Abcd<E extends Comparable<E>>{
......
......
......
}
public class AbcdImpl<E extends Comparable<E>> implements Abcd<E>{
......
......
......
}
Can you explain what <E extends Comparable<E>> stands for and typically what does <E> signify?
回答1:
E stand for a type, the letter doesn't need to be E it can be anything, but usually people will use letters like T,K,E,V. It makes sense to use a letter that somehow signifies what the type is used for. For instance in the Map interface Map<K,V>, K is for key and V is for value.
As for <E extends Comparable<E>> it means that the type E that is the generic type of AbcdImpl needs to itself implement the Comparable interface (for itself). So any class E has to have a method
compareTo(E obj)
for comparing one instance of E to another instance of E.
回答2:
What you see is a generic type declaration.
See http://en.wikipedia.org/wiki/Generics_in_Java
Abcd<E> is normally read as "Abcd of E".
For example, List is a list of items, but it's not specified what kind of items. List is a list of strings, and is a parametrised version of the generic type List.
回答3:
I think you should take a look Lesson: Generics
It will help you for understanding of Generic collection.
来源:https://stackoverflow.com/questions/12108118/what-do-the-angle-brackets-mean-in-this-interface-definition