黑马程序员——泛型
来源:互联网 发布:淘宝极有假货有假货吗 编辑:程序博客网 时间:2024/05/29 18:42
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
了解泛型
ArrayList<E>类定义和ArrayList<Integer>类引用中涉及以下术语:
》整个称为ArrayList<E>泛型类型。
》ArrayList<E>中E称为类型变量或类型参数。
》整个ArrayList<Integer>称为参数化的类型。
》ArrayList<Integer>中的Integer称为类型参数的实例或实际参数类型。
》ArrayList<Integer>中的< > 念为type of。
》ArrayList称为原始类型。
参数化类型与原始类型的兼容性:
》参数化类型可以引用一个原始类型的对象,例如:
Collection<String> c = new Vector();
》原始类型可以引用一个参数化类型的对象,例如:
Collection c = new Vector<String>();
参数化类型不考虑参数的继承关系:
》Vector<String> v = new Vector<Object>();错误
》Vector<Object> v = new Vector<String>();错误
在创建数组实例时,数组的元素不能使用参数化的类型,例如下面语句有错误:
》Vector<Integer> vectorList[] = new Vector<Integer>[6];
泛型中的?通配符:
》使用?通配符可以引用其他各种参数化的类型,?通配符定义的变量主要用作引用,可以 调用与参数化无关的方法,不能调用有关的。
泛型例题:
package cn.itcast.day2;import java.lang.reflect.Method;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.Set;import java.util.Vector;import cn.itcast.day1.ReflectPoint;public class GenericTest {/** * @param args */public static void main(String[] args) throws Exception{// TODO Auto-generated method stub/*ArrayList collection1 = new ArrayList();collection1.add(1);collection1.add(1L);collection1.add("abc");int i = (Integer)collection1.get(1);*/ArrayList<String> collection2 = new ArrayList<String>();collection2.add("abc");collection2.add("efg");collection2.add("hij");System.out.println(collection2.get(1));collection2.getClass().getMethod("add", Object.class).invoke(collection2, 123);System.out.println(collection2);//泛型集合的综合应用案例HashMap<String,Integer> maps = new HashMap<String,Integer>();maps.put("Wade", 28);maps.put("Paul", 25);maps.put("Bryant", 34);Set<Map.Entry<String,Integer>> entrySet = maps.entrySet();for(Map.Entry<String, Integer> entry : entrySet){System.out.println(entry.getKey()+":"+entry.getValue());}//调用自定义泛型方法String[] s = new String[]{"haha","heihei","hiahia"};for(String s1 : s){System.out.println(s1);}swap(s,1,2);for(String s1 : s){System.out.println(s1);}/*swap(new int[]{1,2,3,4});基本数据类型不能作为泛型方法的实际参数,只有引用类型才能作为泛型方法的实际参数。*/GenericDao<ReflectPoint> dao = new GenericDao<ReflectPoint>();dao.add(new ReflectPoint(3, 3));//通过反射的方法来拿到泛型里边的时间类型。Method applyMethod = GenericTest.class.getMethod("applyVector", Vector.class);Type[] types = applyMethod.getGenericParameterTypes();ParameterizedType pType = (ParameterizedType)types[0];System.out.println(pType.getRawType());System.out.println(pType.getActualTypeArguments()[0]);}public static void applyVector(Vector<Date> v1){}//自定义泛型方法private static <T> void swap(T[] a,int i,int j){T temp = a[i];a[i] = a[j];a[j] = temp;}}
package cn.itcast.day2;import java.util.Set;//dao-->data access object-->crud对数据库的增删改查public class GenericDao<E> {public void add(E x){}public E findById(int id){return null;}public void delete(E obj){}public void delete(int id){}public void update(E obj){}public Set<E> findByConditions(String where){return null;}public E findByUserName(String name){return null;}}
- 黑马程序员—泛型
- 黑马程序员—泛型
- 黑马程序员—泛型
- 黑马程序员—泛型
- 黑马程序员—泛型
- 黑马程序员—泛型
- 黑马程序员——泛型
- 黑马程序员——泛型
- 黑马程序员——泛型
- 黑马程序员——泛型
- 黑马程序员——泛型
- 黑马程序员——泛型
- 黑马程序员——泛型
- 黑马程序员——泛型
- 黑马程序员——泛型
- 黑马程序员——泛型
- 黑马程序员——泛型
- 黑马程序员——泛型
- JSP账号密码验证提示
- How uncaught exceptions are handled
- hdu1175
- JAVA的面向对象编程--------课堂笔记
- Win7优化加速[1][珍藏版]
- 黑马程序员——泛型
- tomcat虚拟目录配置
- 程序员面试题精选100题
- automake 搞定-感谢Vin Say
- PHP判断某字符串是否存在于字符串中
- Part1:单例多线程执行模式
- WPF 中DataGrid控件绑定数据的一个例子
- 高性能运算之博科交换机rx16配置
- 插入排序+二分查找