Java泛型(通配符) [英] Java Generics (Wildcards)

查看:80
本文介绍了Java泛型(通配符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中我有几个关于泛型通配符的问题:
$ b

I have a couple of questions about generic wildcards in Java:


  1. 列表与LT ;?延伸T> 列表< ;? super T>

什么是有界通配符?什么是无界通配符? b $ b

What is a bounded wildcard and what is an unbounded wildcard?


推荐答案

在第一个问题中,<扩展T> <? super T> 是有界通配符的例子。无界通配符看起来像<?> ,并且基本上意味着< ;?扩展对象> 。它松散地表示泛型可以是任何类型。有界通配符(< ;? extends T> <?super T> )对类型通过说它要么扩展特定类型(< ;? extends T> 被称为上限),要么必须是一个特定类型的祖先(< ;? super T> 被称为下限)。

In your first question, <? extends T> and <? super T> are examples of bounded wildcards. An unbounded wildcard looks like <?>, and basically means <? extends Object>. It loosely means the generic can be any type. A bounded wildcard (<? extends T> or <? super T>) places a restriction on the type by saying that it either has to extend a specific type (<? extends T> is known as an upper bound), or has to be an ancestor of a specific type (<? super T> is known as a lower bound).

Java教程在通配符中对泛型有一些非常好的解释, a>和更多有趣的通配符

The Java Tutorials have some pretty good explanations of generics in the articles Wildcards and More Fun with Wildcards.

这篇关于Java泛型(通配符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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