"E","T"和“"之间有什么区别? Java泛型? [英] What is the difference between 'E', 'T', and '?' for Java generics?
问题描述
我遇到了这样的Java代码:
I come across Java code like this:
public interface Foo<E> {}
public interface Bar<T> {}
public interface Zar<?> {}
以上三者之间有什么区别,它们在Java中如何称呼这种类型的类或接口声明?
What is the difference among all three of the above and what do they call this type of class or interface declarations in Java?
推荐答案
前两者没有区别-它们只是为 type参数使用了不同的名称(E
或T
).
Well there's no difference between the first two - they're just using different names for the type parameter (E
or T
).
第三个不是有效的声明-?
用作通配符,在提供类型 argument 时使用,例如List<?> foo = ...
表示foo
引用某种类型的列表,但我们不知道什么.
The third isn't a valid declaration - ?
is used as a wildcard which is used when providing a type argument, e.g. List<?> foo = ...
means that foo
refers to a list of some type, but we don't know what.
所有这些都是泛型,这是一个很大的话题.您可能希望通过以下资源来了解它,尽管当然还有更多可用的方法:
All of this is generics, which is a pretty huge topic. You may wish to learn about it through the following resources, although there are more available of course:
- 有关泛型的Java教程
- 泛型语言指南
- Java编程语言中的泛型
- Angelika Langer的 Java泛型常见问题解答(内容广泛,内容广泛,仅供参考)
- Java Tutorial on Generics
- Language guide to generics
- Generics in the Java programming language
- Angelika Langer's Java Generics FAQ (massive and comprehensive; more for reference though)
这篇关于"E","T"和“"之间有什么区别? Java泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!