"E","T"和“"之间有什么区别? Java泛型? [英] What is the difference between 'E', 'T', and '?' for Java generics?

查看:182
本文介绍了"E","T"和“"之间有什么区别? Java泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这样的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参数使用了不同的名称(ET).

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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆