读书人

JAVA泛型范例解析

发布时间: 2012-12-23 11:28:15 作者: rapoo

JAVA泛型实例解析

JAVA泛型大体分为:基本泛型和通配符泛型,在大多数项目下一般使用基本泛型的概率会大一些,此文主要就是对最近学习的泛型进行的一个总结,不足的望给予批评与指导!下例子

?? 案例1:

?

public class GenericTest {/*在JAVA1.4中的集合应用是无泛型的,我们可以向内部传入各种种类的值*/public void Generic14(){ArrayList co = new ArrayList();co.add(1);co.add(1L);co.add("aaa");/*但是我们需要在获取的时候对其进行强制类型转换*/int i = (Integer)co.get(0);System.out.println(i);}/*JDK1.5中添加了泛型关键字为<>(如<String>,其中放置的基本数据类型)这样有效的减少强制转换*/public void Generic15() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{ArrayList<Integer> cos = new ArrayList<Integer>();cos.add(1);cos.add(2);/*因为经过泛型所以cos.add("abc");不能被编译器所通过,但是泛型的样式在编译过后将被剔除仍为ArrayList形式*//*以下为反射的例子,去做验证,利用反射的方法调用ArrayList的add方法向cos对象中插入字符串abc*/Method  method = ArrayList.class.getMethod("add", Object.class);//           对象   参数值method.invoke(cos, "abc");//遍历cos对象for(Object c : cos){System.out.println(c);}}/*例子2*/public void GenericlA(){List<Integer> A = new ArrayList<Integer>();List<String> B = new ArrayList<String>();//泛型在编译时已经被去除并不带入到运行过程System.out.println(A.getClass()==B.getClass());}public void Connection(){//Collection父类Collection c = new ArrayList();//Vector类型相当于Arraylist内部进行了syn的处理,效率会相对Array的类型要底Vector<String> d = new Vector();}public static void main(String[] args) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {GenericTest genericTest = new GenericTest();//genericTest.Generic14();//genericTest.Generic15();//genericTest.GenericlA();genericTest.GAll();}public static void GAll(){//map的迭代HashMap<String, Integer> hashMaps = new HashMap<String, Integer>();hashMaps.put("dhy", 11);hashMaps.put("slb",5);//但是map型不能直接进行迭代必须转化为set类型才能Set<Map.Entry<String, Integer>> entrySet = hashMaps.entrySet();for (Entry<String, Integer> entry : entrySet) {System.out.println(entry.getKey()+" : "+entry.getValue());}}}

??? 案例2:

public class GenericWTest {public static void printCollection (Collection<Object> collection){for(Object c : collection){System.out.println(c);}}public static void main(String[] args) {ArrayList<String> test = new ArrayList<String>();Collection<String> cn = new ArrayList<String>();/* 本身定义的是Collection<Object> * 传入的是ArrayList(Collection的子类型)<String(Object的子类型)> * 但是却说类型不对,原因在于Collecttion<Object>这样定义后就是要传入<object>的值 * 所以在泛型中没有父类与子类之分,所以要用下面的方法 */ArrayList<Object> test2 = new ArrayList<Object>();test.add("a");test.add("b");printCollection(test2);String[] strs = new String[]{"aaa","bbb","ccc"};ArrayList lists = new ArrayList();lists = (ArrayList) copy1(strs, lists);for (Object list : lists) {System.out.println(list);}//泛型的追溯特性,String和Date的都是Serializable的子类copy(new String[10], new Date[10]);//      泛型的传播特性,其中为Date类型()//copy1(new String[10],new ArrayList<Date>());}/*为此推出了?这个通配符*/public static void printCollection2 (Collection<?> collection){/*但是引入这个通配符后,不能对这样的Collection对象去调用与参数有关的方法*//*collection.add("aa");这样的语句是报错的,因为他不能确定泛型是什么类型的*/for(Object c : collection){System.out.println(c);}}//13:servlet jsp jmail jdbc jndi xml public static void ClassTest(){Class<?> a = null;}// 泛型方法public static <T> T[] copy(T[] forms,T[] to){return null;}public static <T> Collection<T> copy1(T[] froms,Collection<T> to){for(T yuan : froms){to.add(yuan);}return to;}}

读书人网 >编程

热点推荐