Java Generics WildCard:< ;?扩展Number> vs< T扩展数字> [英] Java Generics WildCard: <? extends Number> vs <T extends Number>
本文介绍了Java Generics WildCard:< ;?扩展Number> vs< T扩展数字>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这两个函数有什么不同?
static void gPrint(List< ;? extends Number> l){
for(Number n:l){
System.out.println(n);
}
}
static< T extends Number> void gPrintA(List< T> 1){
for(Number n:1){
System.out.println(n);
}
}
我看到相同的输出。
解决方案
在这种情况下没有区别,因为 T
永远不会再次使用。
声明 T
的原因是您可以再次引用它,从而绑定两个参数类型,或一起返回类型。
What is the difference between these 2 functions?
static void gPrint(List<? extends Number> l) {
for (Number n : l) {
System.out.println(n);
}
}
static <T extends Number> void gPrintA(List<T> l) {
for (Number n : l) {
System.out.println(n);
}
}
I see the same output.
解决方案
There is no difference in this case, because T
is never used again.
The reason for declaring a T
is so that you can refer to it again, thus binding two parameter types, or a return type together.
这篇关于Java Generics WildCard:< ;?扩展Number> vs< T扩展数字>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文