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

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

问题描述

我有几个关于 Java 中的通用通配符的问题:

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

  1. List<有什么区别?扩展 T>List?

什么是有界通配符,什么是无界通配符?

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

推荐答案

在你的第一个问题中, 是有界通配符的例子.无界通配符看起来像 ,基本上意味着 .它松散地意味着泛型可以是任何类型.有界通配符()通过说它必须extend 特定类型( 称为上限),或者必须是特定类型的祖先(代码>被称为下界).

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 教程在文章 通配符通配符带来更多乐趣.

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

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

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