黑马程序员技术交流社区

标题: 关于泛型的问题 [打印本页]

作者: 高鑫    时间: 2012-6-14 18:23
标题: 关于泛型的问题
static <T extends Object & Comparable<? super T>>  T  max(Collection<? extends T> coll)
这个是Contactions中的max方法, T extends Object & Comparable中,我认为T是Object和Comparator的的子类,请问这个T和<? super T>中的T是一个T吗??还有Collection<? extends T> 的T也是一个T 吗??
作者: 张_涛    时间: 2012-6-14 18:24
public static <T> void sort(List<T> list, Comparator<? super T> c)这里面一共有三个<T>:
1. 第一个<T>假设作为返回类型的,那么当你定义一个List<Person>并将其返回,那么这个<T>你可以写为<Person>
2.第二个是你待排序的链表内元素数据类型,那么如果元素类型是<Dog>,那你<T>就是<Dog>了
3.应该是一个函数,那么表示这个函数接受参数的类型
当你进行对<Person>排序是,那么这三个<T>,那么你传入的返回的都应该是<Person>
当你对<Dog>排序是,那么这三个<T>,那么你传入的返回的都应该是<Dog>
作者: 姚玉鹏    时间: 2012-6-14 19:17
public static <T extends Object & Comparable<? super T>> T max(Collection<T> coll)

这是一个对一个类型参数给定多个界限(multiple bounds)的例子,是用语法 T1 & T2 … & Tn。一个有多个界限的类型的参数是所有界限中列出来的类型的子类。当多个界限被使用的时候,界限中的第一个类型被用作这个类型参数的erasure。

(原文:This is an example of giving multiple bounds for a type parameter, using the syntax T1& T2 ... & Tn. A type variable with multiple bounds is known to be a subtype of all of the types listed in the bound. When a multiple bound is used, the first type mentioned in the bound is used as the erasure of the type variable.)
都是一个T
希望对你有帮助




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2