泛型的使用原则:Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used.
关于<? extends T>和<? super T>的使用原则:In general, if you have an API that only uses a type parameter T
as an argument, its uses should take advantage of lower bounded wildcards (? super T
). Conversely, if the API only returns T
, you'll give your clients more flexibility by using upper bounded wildcards (? extends T
).
if you have an API that only uses a type parameter T
as an argument, its uses should take advantage of lower bounded wildcards (? super T
).
Conversely, if the API only returns T
, you'll give your clients more flexibility by using upper bounded wildcards (? extends T
).:
这种情况就好像,假如T是个接口,你传入的数据是实现了这个接口的类,在方法中做完处理后,作为返回值再返回去。因为返回值的类型是T,所以和传数据同样类型的数据做为返回值没有问题。
官方教程:
http://docs.oracle.com/javase/tutorial/java/generics/index.html
日本教程(非常详细,把官方举的例子的原理都说明白了):
http://www.ne.jp/asahi/hishidama/home/tech/java/generics.html#Generic_型引数
国内教程:
http://www.cnblogs.com/mengdd/archive/2013/01/21/2869778.html
http://www.cnblogs.com/lwbqqyumidi/p/3837629.html
http://www.cnblogs.com/iyangyuan/archive/2013/04/09/3011274.html