“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
).
第三个不是有效的声明 - ?
用作通配符,用于提供类型参数,例如<代码>列表>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屋!