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

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

问题描述

  public interface Foo< E> {} 

public interface Bar< T> {}

public interface Zar<?> {}

以上所有三点之间的区别是什么,他们称这种类型为什么或接口声明在Java?

解决方案

那么前两个之间没有区别 - 他们只是使用不同的名称 type parameter E T )。



第三个不是有效的声明 - 用作通配符,用于提供类型参数,例如列表与LT;?> foo = ... 表示 foo 指的是某种类型的列表,但我们不知道是什么。



所有这些都是泛型,这是一个相当大的话题。您可能希望通过以下资源了解它,但当然还有更多可用:


I come across Java code like this:

public interface Foo<E> {}

public interface Bar<T> {}

public interface Zar<?> {}

What is the difference among all three of the above and what do they call this type of class or interface declarations in Java?

解决方案

Well there's no difference between the first two - they're just using different names for the type parameter (E or T).

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泛型的'?','E'和'T'有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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