java增强之泛型练习2:泛型DAO、通过反射获取泛型参数

来源:互联网 发布:重庆宏宇软件 编辑:程序博客网 时间:2024/06/10 00:23
//任务6:利用反射获取方法的泛型参数、泛型异常、泛型返回值public <T> void applyList(List<T> list) throws Exception{/** * 思路: * 1.获取方法 * 2.获取方法的泛型参数 */Method method = GenericTest.class.getMethod("applyList", List.class);//知识点:参数化的类型,ParameterizedType/* * 下面是错误的 * ParameterizedType[] pt= (ParameterizedType[]) method.getGenericParameterTypes(); */Type[] types=method.getGenericParameterTypes();//获取types中的元素,转成ParameterizedType这种类型ParameterizedType pt=(ParameterizedType) types[0];System.out.println(pt.getRawType()+";"+pt.getActualTypeArguments()[0]);}public void testApplyList() throws Exception{applyList(new ArrayList<Integer>());}


二 泛型DAO

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 static <E> void update2(E obj){}public E findByUserName(String name){return null;}public Set<E> findByConditions(String where){return null;}}


0 0