Java – generic type parameters depend on itself
•
Java
I encountered the following situations:
I understand this:
In the type parameter part of the class, if s is the boundary of T, the type variable t depends directly on the type variable s, and if t depends directly on s or T depends directly on the type variable U, t depends on S. s (recursion uses this definition)
but
If the type variable in the type parameter part of a class depends on itself, then this is a compile - time error
What's the meaning of this? Reference
Solution
This statement means that a type parameter variable cannot depend on itself The following codes are not allowed:
class Generic<T extends T> { }
Here t is a type parameter variable, which cannot depend on itself (direct or indirect) On the other hand, the following codes are allowed:
public class GenericRecursive<T extends GenericRecursive<T>> { }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码