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

查看:28
本文介绍了“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).

第三个不是有效的声明 - ? 用作通配符,用于提供类型参数,例如<代码>列表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天全站免登陆