Java generics - How do I read this: Foo<T extends Bar<? extends Foo<T>>>?
I am bringing myself up to date with Java generics (having worked for a loooong time on legacy code with JDK 1.4... 1.3 even) and I don't quite understand this:
public class Foo<T extends Bar<? extends Foo<T>>> { ...
Where Foo
and Bar
are two generic classes.
开发者_如何学GoHow is this to be understood because I don't quite get it?
I've read a lot about Java generics but this is a little mind bending (at least for me as a beginner).
Well, Foo
has to be parameterized by a T
. That T
itself has to extend Bar<U>
for some type U
such that U
extends Foo<T>
. (Where "extend" can mean "is the same type as" in this case.) I've used U
here as an arbitrary type name, but it's unnamed in the declaration, hence ?
.
You're right that it's a bit mind-bending, but typically in the circumstances where this sort of thing crops up, it ends up making things simpler. If you can give a concrete example, we may be able to explain a bit more usefully.
精彩评论