`copy(List<?super T> dest,List&lt ;? extends T> src)`和`copy(List< T&

编程入门 行业动态 更新时间:2024-10-28 02:36:09
本文介绍了`copy(List<?super T> dest,List&lt ;? extends T> src)`和`copy(List< T> dest,List&lt ;? extends T> src)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图通过阅读以下内容来学习Java泛型通配符: http:// www.angelikalanger/GenericsFAQ/FAQSections/TypeArguments.html#FAQ103

材料中有一个例子:

public class Collections { public static< T> void copy(List< ;? super T> dest,List< ;? extends T> src){ for(int i = 0; i< src.size(); i ++) dest.set(i ,为src.get(I)); $ / code>

我想知道是否可以将方法签名更改为如下所示:

public static< T> void copy(List< ;? super T> dest,List< ;? extends T> src){

public static< T> void copy(List< T> dest,List< ;? extends T> src){

这两个方法之间的区别是什么?

示例将不胜感激。 p>你是对的。在这种情况下,两个参数的类型参数用于表示 dest 必须包含 src 。因此,如果您说 src 包含<?如果 $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $ T $您也可以用其他方式表达,即:

列表<?超T> dest,List< T> src

达到同样的效果。

编辑:我怀疑作者强调了关于 PECS原则

I am trying to learn Java Generics wildcard by reading the following: www.angelikalanger/GenericsFAQ/FAQSections/TypeArguments.html#FAQ103

There is one example in the material:

public class Collections { public static <T> void copy (List<? super T> dest, List<? extends T> src) { for (int i=0; i<src.size(); i++) dest.set(i,src.get(i)); } }

I was wondering if I can change the method signature as the following:

public static <T> void copy(List<? super T> dest, List<? extends T> src) {

public static <T> void copy(List<T> dest, List<? extends T> src) {

Are there any differences between these two method sinatures?

Examples would be appreciated.

解决方案

You are correct. In this case the two parameter's Type Arguments are being used to express the relationship that dest must contain objects of a super type of the objects in src. Therefore if you say src contains <? extends T> then it's sufficient to say that dest contains objects of T.

You can also express it the other way round, namely:

List<? super T> dest, List<T> src

to the same effect.

EDIT: I suspect the author to reinforce the point about the PECS principle

更多推荐

`copy(List&lt;?super T&gt; dest,List&lt ;? extends T&gt; src)`和`

本文发布于:2023-11-07 04:33:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565572.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:amp   lt   copy   List   super

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!