标签:style class blog code java http
关于泛型的基本介绍和理解请参考以下几篇文章,或查询更多资料:
本篇以简单的List<>方式来进行说明。
ArrayList<Object>继承了List<Object>,
ArrayList<String>没有继承List<Object>
List<?>等价于List<? extends Object>
请参考以下代码:
/** * author Ding Chengyun * 2014-2-27 */ package test; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * @author Ding Chengyun * 2014-2-27L */ public class ListTest { public void method1(List<Object> list) { } public void method2() { method1(new ArrayList<Object>()); method1(new ArrayList<String>()); //会编译报错 method1(new LinkedList<Object>()); } public void method3(List<? extends Object> list) { } public void method4() { method3(new ArrayList<Object>()); method3(new ArrayList<String>()); method3(new LinkedList<Integer>()); } public void method5(List<?> list) {//泛型中?等价于? extends Object } public void method6() { method5(new ArrayList<Object>()); method5(new ArrayList<String>()); method5(new LinkedList<Integer>()); } /** * @param args */ public static void main(String[] args) { List<String> list = new ArrayList<String>(20); } }
标签:style class blog code java http
原文地址:http://blog.csdn.net/sundenskyqq/article/details/27788705